Problem Solving with C++ (9th Edition)
Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 14, Problem 5P

Explanation of Solution

Recursive function for checking palindrome:

The recursive function definition for checking the given string is a palindrome or not is shown below:

/* Function definition for checkPalindromeRecursion function */

bool checkPalindromeRecursion(string str)

{

      /* If length is "0" or "1", then return "true" */

      if(str.length() == 0 || str.length() == 1)

            return true;

        /* Check the first character*/

        if(isdigit(str.at(0)))

        {

     /* If the first character is equal to the last character, then */

              if(str.at(0) == str.at(str.length()-1))

     /* Recursively call the function "checkPalindromeRecursion" with remaining characters */

       return checkPalindromeRecursion(str.substr(1, str.length()-2));

        }

        //Otherwise

        else

        {

              //For checking the lower characters

     if(tolower(str.at(0)) == tolower(str.at(str.length()-1)))

     return checkPalindromeRecursion(str.substr(1, str.length()-2));

        }

        //Otherwise returns "false"

        return false;

}

Explanation:

The above function is used to check the given string is a palindrome or not using recursive function.

  • If the length of string is “0” or “1”, then returns “true”.
  • Check the first digit of given string.  If it is, then check if the first character is equal to the last character, then recursively call the function “checkPalindromeRecursion” for remaining characters in given string.
  • Otherwise, return “false” that is for not palindrome string.

Complete Executable code:

The complete code is implemented for checking the palindrome is shown below:

//Header file

#include <iostream>

#include <iomanip>

//For standard input and output

using namespace std;

/* Function definition for checkPalindromeRecursion function */

bool checkPalindromeRecursion(string str)

{

      /* If length is "0" or "1", then return "true" */

      if(str...

Blurred answer
Students have asked these similar questions
Write a recursive function that returns true if the digits of a positive integer are in increasing order; otherwise, the function returns false. Also, write a program to test your function.
Write a recursive function for int powerOfTwo (int k). The function determines the value of 2k. (Note: k is a positive integer). Example, when k=0 the function returns 1 and when k-3 the function returns 8. To show that your code is correct, give the recursive trace for powerOfTwo (2) that returns 4.
Recursive Palindrome! Recall that a palindrome is a string that reads the same forward and backward. Write a recursive function is_palindrome (s:str) -> bool to check whether a string s is a palindrome. Here's a hint: think about how you can use is_palindrome(t), where t is a substring of s, to help you decide whether s is a palindrome. Your Answer: 1 # Put your answer here 2 Submit
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning