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

Implement a function to recursively determine if a word is a palindrome. A palin

ID: 3819781 • Letter: I

Question

Implement a function to recursively determine if a word is a palindrome. A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction. Famous examples include "Able was I ere I saw Elba", "A man, a plan, a canal: Panama".

Important questions to ask yourself:

What is the base case?

What is the recursive case?

The substr method of the string class will be useful. It takes the starting position of the first character to be copied as a substring and the number of characters to include in the substring.
Also useful is the length method. It returns the length of the string in question.

Examples:

Here is some code to get you started. Submit your modified palindrome function. Do not modify the main function. When testing your function don't forget to type the word on the command line after "a.out": ./a.out radar

My professor gave me this code to start off with.

Palindrome Tester

Implement a function to recursively determine if a word is a palindrome. A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction. Famous examples include "Able was I ere I saw Elba", "A man, a plan, a canal: Panama".

Important questions to ask yourself:

What is the base case?

What is the recursive case?

The substr method of the string class will be useful. It takes the starting position of the first character to be copied as a substring and the number of characters to include in the substring.
Also useful is the length method. It returns the length of the string in question.

Examples:

  string s = "defiant";  cout << s.substr(2, s.length()-4) << endl;  
The above example prints out: fia

Here is some code to get you started. Submit your modified palindrome function. Do not modify the main function. When testing your function don't forget to type the word on the command line after "a.out": ./a.out radar

Explanation / Answer

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

bool palindrome(string);

int main(int argc, char *argv[]) {

string s = argc == 2 ? argv[1] : "redivider";

if (palindrome(s)) {
cout << """ << s << "" is a palindrome." << endl;
}
else {
cout << """ << s << "" is not a palindrome." << endl;
}

return 0;
}

bool palindrome(string w) {

int len = w.length();

string start = w.substr(0,1);
string end = w.substr((len - 1), 1);

if (start == end)
{
w = w.substr((0 + 1), (len - 2));
if (w.length() <= 1) return true;
return palindrome(w);
}
else
return false;

return false;
}

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