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
You are asked to explain what a computer virus is and if it can affect computer’shardware or software. How do you protect your computer against virus? give one reference with your answer.
Distributed Systems: Consistency Models fer to page 45 for problems on data consistency. structions: Compare different consistency models (e.g., strong, eventual, causal) for distributed databases. Evaluate the trade-offs between availability and consistency in a given use case. Propose the most appropriate model for the scenario and explain your reasoning. Link: [https://drive.google.com/file/d/1wKSrun-GlxirS31Z9qoHazb9tC440AZF/view?usp=sharing]
Operating Systems: Deadlock Detection fer to page 25 for problems on deadlock concepts. structions: • Given a system resource allocation graph, determine if a deadlock exists. If a deadlock exists, identify the processes and resources involved. Suggest strategies to prevent or resolve the deadlock and explain their trade-offs. Link: [https://drive.google.com/file/d/1wKSrun-GlxirS31Z9qoHazb9tC440 AZF/view?usp=sharing]
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