A palindrome is a string that reads the same both forward and backward. For exam
ID: 3674829 • Letter: A
Question
A palindrome is a string that reads the same both forward and backward. For example, the string "madam" is a palindrome. Alan has sent you a program that uses a recursive function to check whether a string is a palindrome. The program contains a value-returning recursive function that returns true if the string is a palindrome and false otherwise. However, he can not get the parameters in the recursive function call to work quite right. Download the source code and review the code. Modify this one line...the last statement of the isStringPalindrome function.
return isStringPalindrome(); //Add the parameters that will solve this problem
so that it has the correct parameters. In C++
Explanation / Answer
// Hi i have done your requirement. You can test.
#include "stdafx.h" //If you are using Visual Studio, if not comment out
#include <iostream>
#include <iomanip>
#include <string>
#include <conio.h>
using namespace std; //no need to use std::cout ... in your code
bool isPalindrome(string str);
bool isStringPalindrome(string str, int first, int last);
//first is the position of the first character of the string being passed
//last is the position of the last character of the string being passed
//This is the main driver - Do not modify
int main()
{
string str;
char q = 'Y';
cout << "================================================== ";
cout << " Palindrome App. Modified by YourName. Due Date ";
cout << "================================================== ";
do{
cout << " Enter a string: ";
getline(cin,str);
cout << endl;
if (isPalindrome(str))
cout << str << " is palindrome" << endl;
else
cout << str << " is not a palindrome" << endl;
cout << " Do you want to run the program again? (y or n): ";
cin >> q;
cin.ignore(100, ' ');
}while(q != 'n' && q != 'N');
cout << " Press any key . . .";
_getch();
return 0;
}
//Do not modify this function
bool isPalindrome(string str)
{
return isStringPalindrome(str,0,str.length() - 1);
}
//Modify this function
bool isStringPalindrome(string str, int first, int last)
{
if ((last - first == 0))
return true;
else if (last - first == 1)
{
if (str[first] == str[last])
return true;
else
return false;
}
else if (str[first] != str[last])
return false;
else
return isStringPalindrome(str, first+1, last-1); //Add the parameters that will solve this problem
}
/*
Output:
==================================================
Palindrome App. Modified by YourName. Due Date
==================================================
Enter a string: madam
madam is palindrome
Do you want to run the program again? (y or n): y
Enter a string: loopoop
loopoop is not a palindrome
Do you want to run the program again? (y or n): y
Enter a string: loopool
loopool is palindrome
Do you want to run the program again? (y or n): n
Press any key . . .
------------------
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.