Problem Solving with C++ plus MyProgrammingLab with Pearson eText-- Access Card Package (9th Edition)
Problem Solving with C++ plus MyProgrammingLab with Pearson eText-- Access Card Package (9th Edition)
9th Edition
ISBN: 9780133862218
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, reverseDigits, that takes an integer as a parameter and returns the number with the digits reversed. Also, write a program to test your function.
Write a recursive fibonacci (n) function with an expression body.  The function should return an Int; it will be too slow to deal with inputs whose fibonacci numbers are too large anyway. You do *not* need to use memoization.    Note that: fibonacci(0) = 0 fibonacci(1) = 1 fibonacci(n, where n is greater than 1) = fibonacci(n-2) + fibonacci(n - 1)
Knowledge Booster
Background pattern image
Similar 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