C++ Palindrome is a sequence of characters or numbers which reads the same backw
ID: 3930620 • Letter: C
Question
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.
Explanation / Answer
#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)) << " ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.