Write a program that reads in any number of MyStrings of characters from the user and determines if each MyString is a palindrome. The user will terminate each MyString by pressing the return (or enter) button. The user will indicate that there are no more MyStrings by entering "quit". The function must be recursive and must not call any other functions.

Respuesta :

Answer:

Here is the recursive function:

bool isPalindrome(string& MyString,int start, int end){    // function that takes as argument a string and two arguments that are bounds indices

       while(start < MyString.length() && !isalpha(MyString[start])){  //while loop continues to execute as long as start does not exceed the length of the string AND does not point to a alphabetic letter

           start++;         } // increments start by 1 moving it to point the next  character

       while(end >= 0 && !isalpha(MyString[end])){  //while loop continues to execute as long as end does not gets less than 0 AND does not point to a alphabetic letter

           end--;         }    // decrements end by 1 moving it to point the next  character backwards

       if(start > end){  //the base case

           return true;        

       } else {    //recursive case

           return ( ( toupper(MyString[start]) == toupper(MyString[end]) ) && isPalindrome(MyString,start+1,end-1));         }    }  //The recursive case is to AND the result of comparing the  alphabetic letters at start and end with result of the function  (called recursively) after without both of the letters

Explanation:

Here is the complete C++ program:

#include <iostream>  //to use input output functions

#include <cctype>  //functions to manipulate characters

using namespace std; //to identify objects as cin cout

bool isPalindrome(string&,int start, int end);  // function prototype

int main(){  //start of main function

       string MyStrings;        //declare a string type variable to hold string of characters

       cout<<"Enter a string : ";  //prompts user to enter a string

       getline(cin,MyStrings );  //gets and reads the string from user

       while(MyStrings != "quit"){        // continues to take input from user until user enters quit

       if(isPalindrome(MyStrings ,0,MyStrings.length()-1)== true){  //calls function that checks if the string is a palindrome

       cout<<MyStrings<<" is a Palindrome\n";}  //if the input string is a palindrome then display this message

       else  

       {cout<<MyStrings<<" is not a Palindrome\n";}  //if the input string is not a palindrome then displays this message

       cout<<"Enter a string : ";  //prompts user to enter a string

       getline(cin,MyStrings); }    }        //reads a string from user

   bool isPalindrome(string& MyString,int start, int end){  //function definition        

       while(start < MyString.length() && !isalpha(MyString[start])){  

           start++;         }          

       while(end >= 0 && !isalpha(MyString[end])){

           end--;         }        

       if(start > end){

           return true;        

       } else {              

           return ( ( toupper(MyString[start]) == toupper(MyString[end]) ) && isPalindrome(MyString,start+1,end-1));         }    }

             

I will explain the working of the function:

First the start variable is pointed to the start of the MyString and the end variable points to the last character of MyString    

while(start < MyString.length() && !isalpha(MyString[start]))

The above statement has a while loop which continues to execute as long as start does not exceed the string length and  it does not point to a letter. It has a AND logical operator that means both the above condition have to be true in order to continue this while loop. Here isalpha() method is used that checks if the letter/character of MyString is an alphabet or not. At each iteration start is incremented to 1 to point the next  character of MyString.

while(end >= 0 && !isalpha(MyString[end]))

The above statement has a while loop which continues to execute as long as end does not becomes less than 0 and  it does not point to a letter, Here isalpha() method is used that checks if the letter/character of MyString is an alphabet or not. At each iteration end is decremented to 1 to point the next  character of MyString in backward or in reverse order.

if(start > end){  

The above statement is a base case which is the IF condition that checks if the start is greater than end. This basically shows that there are no more alphabetic characters and this base case returns true as it reads the same from both sides.

else {              

           return ( ( toupper(MyString[start]) == toupper(MyString[end]) ) && isPalindrome(MyString,start+1,end-1));         }    }

The above statement is a recursive part. It uses AND logical operator and compares the characters at start and end with the result of the function  after excluding both of the characters pointed by start and end.

The program along with its output is attached.

Ver imagen mahamnasir