C++ Programming A \'array palindrome\' is an array which, when its elements are
ID: 3853652 • Letter: C
Question
C++ Programming
A 'array palindrome' is an array which, when its elements are reversed, remains the same (i.e., the elements of the array are same when scanned forward or backward) Write a recursive, bool-valued function, isPalindrome, that accepts an integer-valued array, and the number of elements and returns whether the array is a palindrome.
An array is a palindrome if:
the array is empty (0 elements) or contains only one element (which therefore is the same when reversed), or
the first and last elements of the array are the same, and the rest of the array (i.e., the second through next-to-last elements) form a palindrome.
Explanation / Answer
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
int isPalindrome(char* str, int last)
{
if(last < 1)
{
return 1;
}
if(str[0] != str[last])
{
return 0;
}
return isPalindrome(str+1, last-2);
}
void Palindrome(char* str)
{
std::cout<<" The String is: " <<str;
if(!isPalindrome(str, strlen(str)-1))
std::cout<<" Not a Palindrome";
else
std::cout<<" is a palindrome";
}
int main()
{
Palindrome("star rats");
Palindrome("Space Craft");
}
OUTPUT
The String is: star rats is a palindrome
The String is: Space Craft Not a Palindrome
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.