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
What is required for real-time surveillance of a suspects computer activity?/p Group of answer choices a. Blocking data transmis
Hitman42 [59]

Answer:

c  Preventing data transmissions between a suspect’s computer and a network server

Explanation:

3 0
3 years ago
A program that is used to view websites is called a​
Nookie1986 [14]

Answer:

A web browser

examples of this would be

Bing

6 0
3 years ago
Read 2 more answers
Most microsoft windows fatal errors (blue scree of death) are caused by:
MAVERICK [17]
Errors in kernel mode (CPU running in ring 0) are usually fatal. This happens mostly inside device drivers.
7 0
3 years ago
1. Write a 400-500 word research report about Burke High School.
fenix001 [56]

Answer:

when is it due

Explanation:

6 0
3 years ago
What are the main approaches for dealing with deadlock?
alina1380 [7]

Answer:

Deadlock ignorance

Deadlock prevention

Deadlock avoidance

& Detection and recovery

Hope This Helps!!!

3 0
3 years ago
Other questions:
  • How can i put this sign in my keybord?<br><br> :::::<br> ^<br> Here is the sign
    8·1 answer
  • All of the following are types of data storage devices except CD/DVD computer monitor digital cameras flash drive
    15·1 answer
  • Which of the following is a type of monitor port?
    10·1 answer
  • What is the main idea of this article? Please someone help me. I will give brainliest answer
    7·1 answer
  • Why is it important for people to express resentments in the workplace?
    9·1 answer
  • What should you do if an initial search returns too many results on unrelated topics
    15·1 answer
  • A process is: An activity of a function that is performed for some specific business reason A single piece of data within a syst
    14·1 answer
  • Which of the following allows computers to communicate with each other? RAM NIC Hard drive Floppy disk
    11·1 answer
  • Which testing is an example of non-functional testing? A. testing a module B. testing integration of three modules C. testing a
    13·1 answer
  • Assume that you have implemented a sequence class. Describe the mySequence object (i.e., items with the correct order and the po
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!