1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
sineoko [7]
3 years ago
14

Write a program that reads in any number of MyStrings of characters from the user and determines if each MyString is a palindrom

e. 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.

Computers and Technology
1 answer:
lapo4ka [179]3 years ago
4 0

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.

You might be interested in
Interactive sites where users write about personal topics and comment to a threaded discussion is
-BARSIC- [3]
Social media? is that what you were looking for?
6 0
3 years ago
How would you define the rule of thirds?
mixer [17]

A pronciple of composition used fir centuries by artists and photographers or it is a method when used to frame a composition properly

8 0
3 years ago
In which situations would a text-to-speech tool be useful? Check all that apply. O A reader needs to hear a word pronounced. A s
spin [16.1K]

Answer:

The answer to this question is given below in the explanation section

Explanation:

The correct answer is :

A reader needs to hear a word pronounced

A student needs to take notes about a text

Because, text to speech tools mostly used to hear the text written in some files such as reading text from pdf file etc, or word pronunciation. However other options are not right because, reading a difficult material does not make sense with reading it links to understanding.

However, speech to text can be used by the student to take notes while listening the text, but would be easy instead of taking notes and listening, it is easy to copy the text.

7 0
3 years ago
Read 2 more answers
The ability to create slide shows is a major advantage of word processing software.
Gennadij [26K]
The correct answer is false
8 0
3 years ago
Every object in asequence container has a specific position.<br><br> a. True<br><br> b. False
Anastasy [175]

Answer: a)True

Explanation: A sequence container is a class which has the sequence of the objects present in it .By having the objects in a sequence it has a specific position that is given to the objects to keep them in a particular order .the sequence of objects can be like first object, second object , third object ...etc. Therefore the statement given is true that every object in a sequence container has a specific position.

5 0
4 years ago
Other questions:
  • The ___ is the primary storage device of a personal computer. hard drive RAM ROM compact disc
    6·1 answer
  • The most common size for a brochure is _____.
    9·1 answer
  • In JAVA please:
    15·1 answer
  • What is network management?
    12·1 answer
  • Nonverbal messages from the movie it​
    5·2 answers
  • In chapter 3, we discussed syntax and semantics, in general there are two types of grammars for programming languages, regular a
    14·1 answer
  • ¿Cual es la ultima combinacion de celdas en Excel 365?
    7·1 answer
  • Software licensed as proprietary
    11·2 answers
  • David was editing his audio recording in his audio editing software. He adjusted the vocal track of his audio toward
    14·1 answer
  • What are the benefits and drawbacks of a desktop utilising virtualisation and a server?
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!