Write a C++ recursive function to check if a string is palindrome. The function
ID: 672888 • Letter: W
Question
Write a C++ recursive function to check if a string is palindrome. The function has three parameters: a string, an integer start index and an integer end index. The function should return true if the string is palindrome. Otherwise, return false.
bool isPalindrome(const string &str, int start, int end)
For example:
Test Result // This is a Finnish word for soapstone vendor.// According to the Guiness Book of World Records,
// it is the longest palindrome in every day use.
string s = "saippuakivikauppias";
cout<<isPalindrome(s, 0, s.length() - 1); True
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
bool isPalindrome(const string &s,int start,int end)
{
if(((start==end)&&(s[start]==s[end]))||start>end)
return true;
else if(s[start]==s[end])
return isPalindrome(s,start+1,end-1);
else
return false;
}
int main()
{
string s = "saippuakivikauppias";
if(isPalindrome(s, 0, s.length() - 1))
cout<< "Yes";
else
cout << "No";
cout << endl;
s = "Hello";
if(isPalindrome(s, 0, s.length() - 1))
cout<< "Yes";
else
cout << "No";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.