C Palindrome is a sequence of characters or numbers which re

C++

Palindrome is a sequence of characters or numbers which reads the same backward or forward. Examples: abcba, pqrssrqp, 13531 are palindromes

Write a program that checks if a character array is palindrome or not. The size of the array is user-defined in main.

Create a user-defined character array by calling a function createArray. Create another function checkPalindrome that accepts the character array as parameter and returns to main function, a boolean status of the sequence being a palindrome or not. Display the status of the sequence from the main function.

Solution

#include <iostream>
#include <string.h>
using namespace std;

char* createArray(int size){
   char *arr = new char[size];
   cout << \"Enter the character array: \";
   cin >> arr;
   return arr;
}

bool checkPalindrome(char *arr){
   for(int i = 0; i < strlen(arr); i++){
       if(arr[i] != arr[strlen(arr) - i - 1]) return false;
   }
   return true;
}

int main(){
   int n;
   cout << \"Enter the size of the char sequence: \";
   cin >> n;
   cout << checkPalindrome(createArray(n)) << \"\ \";
}

C++ Palindrome is a sequence of characters or numbers which reads the same backward or forward. Examples: abcba, pqrssrqp, 13531 are palindromes Write a program

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site