184k views
5 votes
A palindrome is a string that reads the same both forward and backward. For example, the string "madam" is a palindrome. Write a program that uses a recursive function to check whether a string is a palindrome. Your program must contain a value-returning recursive function that returns true if the string is a palindrome and false otherwise. Do not use any global variables; use the appropriate parameters.

1 Answer

4 votes

// this function will return 1 if the string sent in arguments is palindrome //else it will return 0

int palindrome(int start_of_string, int end_of_string, string &strr)

{

// base case to check if the string is not empty

if (start_of_string>= end_of_string)

return 1;

if (strr[start_of_string] != strr[end_of_string])

return 0;

// recursive call

return palindrome(++start_of_string, --end_of_string, strr);

}

User Jason Bert
by
5.5k points