Question

palindrome is a string that reads the same both forward and backward. C++ For example, the...

palindrome is a string that reads the same both forward and backward.

C++

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution.cpp

#include <iostream>//header file for input output function
#include <cstring>//header file for string functions

using namespace std;//it tells the compiler to link std namespace

bool isPalindrome(char *inputString, int leftIndex, int rightIndex);//function declaration

int main(){//main function
    char Str[100];
    cout<<"Enter a string for palindrome check\n";
    cin>> Str;//keyboard inputting
         //calling function
    if(isPalindrome(Str, 0, strlen(Str) - 1)){
        cout<<Str<<" is a Palindrome \n"<< Str;
    } else {
        cout<<Str<<" is not a Palindrome \n"<< Str;
    }
    return 0;
}

/*
* Function to check whether a string is palindrome or not
*/
bool isPalindrome(char *inputString, int leftIndex, int rightIndex){
     /* Input Validation */
     if(NULL == inputString || leftIndex < 0 || rightIndex < 0){
         cout<<"Invalid Input";
         return false;
     }
     /* Recursion termination condition */
     if(leftIndex >= rightIndex)
         return true;
     if(inputString[leftIndex] == inputString[rightIndex]){
         return isPalindrome(inputString, leftIndex + 1,rightIndex - 1);
     }
     return false;
}

output

madam                                                                                                                                                                    

madam is a Palindrome

Add a comment
Know the answer?
Add Answer to:
palindrome is a string that reads the same both forward and backward. C++ For example, the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT