Question: Write a C++ program that uses recursion to test whether a string is a palindrome (a string that is identical to its reverse, like Ah,

Write a C++ program that uses recursion to test whether a string is a palindrome (a string that is identical to its reverse, like "Ah, Satan sees Natasha".)

Take the input string as a command-line argument. (hints: argv[0] is the executable name, so you want argv[1]; string has a constructor that takes a pointer to c string as an argument.) Also note that you can pass a command line argument that contains whitespace by enclosing it in quotes (e.g. "hey, this string contains whitespace!")

The output should print the raw string (with any punctuation and whitespace) and whether or not the string is a palindrome. For example:

# ./recurs "Madam, I'm Adam" "Madam, I'm Adam" is a palindrome # ./recurs "Madam, I'm Joe" "Madam, I'm Joe" is not a palindrome

Use the following functions from the ctype library (include ) isalpha() returns a boolean that indicates whether a char is an alphabetic character. tolower() converts a char to lower case if necessary. Use these functions with a loop to create a string without punctuation or whitespace and test it, rather than the raw input string.

Note that there are other ways to do this, but do it recursively, but stripping the first and last letters in each instance of the function and comparing them for equality. Make sure the termination condition for the recursion works correctly for strings of both odd and even lengths. Note that you can use negative indices to access items starting with the end of a string or list (eg, my_string[-1]). Return a boolean.

Your program does not need to be object oriented. It should contain a main function that prepares the no-punctuation, all-alphabetic string and calls the recursive function. The recursive function should take a string as argument and return a boolean.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!