Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote