Need a project done?

C++ Programming Developer

Search This Blog

Palindrome Recursion C++

Write a recursive function of determining whether a string is palindrome or not.
Note: A palindrome is a string that can be read the same both forward and backwards such as racecar, mom etc.

Solution:
Explanation: Click here to understand each step.

#include <iostream>
using namespace std;

bool palindrome(char[], int size);

bool main()
{
cout << "Enter a string: ";
char str[20];

cin.getline(str,20,'\n');
cout << "The entered string " << ( (palindrome(str,strlen(str)+1))? "is":"is not" ) << " a Palindrome string." << endl;
//If you didn't understand ?: click here.
return 0;
}

bool palindrome(char str[], int size)
{
if (str[0] != str[size-2]) //if first element is not equal to last element.
return false;
else if (strlen(str) <= 1)
return true;
else
return palindrome(str+1,size-2);
}


"Don't let anyone ever make you feel like you don't deserve what you want."