Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
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, sumDigits, that takes an integer as a parameter and returns the sum of the digits of the integer. 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.
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