جناس قلب رشته ای است که اگر از هر طرف بنویسید یکسان می باشد.به عنوان مثال radar و (اگر فاصله ها را در نظر نگیریم) ((a man a plan a canal Panama)) یک تابع بازگشتی به نام testPalindrome بنویسید که در صورتی که رشته ذخیره شده در ارایه ی جناس قلب است true در غیر این صورت false را برگرداند. این تابع بازگشتی باید فاصله های خالی و علائم نقطه گذاری را در نظر نگیرد.
#include<iostream>
#include<conio.h>
using namespace std;
bool testPalindrome(const char[], int, int);
int main()
{
const int SIZE = 80;
char c, string[SIZE], copy[SIZE];
int count = 0, copyCount, i;
cout << "Enter a sentence:\n";
while((c = cin.get()) != '\n' && count < SIZE)
string[count++] = c;
string[count] = '\0'; //terminate cstring
//make a copy of cstring without spaces
for(copyCount = 0, i = 0; string[i] != '\0'; ++i)
if(string[i] != ' ')
copy[copyCount++] = string[i];
if(testPalindrome(copy, 0, copyCount - 1))
cout << '\"' << string << "\" is a palindrome" << endl;
else
cout << '\"' << string << "\" is not a palindrome" << endl;
getch();
return 0;
}
bool testPalindrome(const char array[], int left, int right)
{
if(left == right || left > right)
return true;
else if (array[left] != array[right])
return false;
else
return testPalindrome(array, left + 1, right - 1);
}