Question

For this assignment, you will create a program that tests a string to see if it...

For this assignment, you will create a program that tests a string to see if it is a palindrome. A palindrome is a string such as “madam”, “radar”, “dad”, and “I”, that reads the same forwards and backwards. The empty string is regarded as a palindrome. Write a recursive function: bool isPalindrome(string str, int lower, int upper) that returns true if and only if the part of the string str in positions lower through upper (inclusive at both ends) is a palindrome. Test your function by writing a main function that repeatedly asks the user to enter strings terminated by the ENTER key. These strings are then tested for palindromicity. The program terminates when the user presses the ENTER key without typing any characters before it. I'm using XCODE.

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

Below is the complete program as per the requirements. It is well explained inside the code using comments. If still, you find any difficulty in understanding this, let me know.

#include <iostream>
#include <string>

using namespace std;

// returns true if and only if the part of string str in positions
// lower through upper inclusive both is a Palindrome, otherwise
// it returns false
bool isPalindrome(string str, int lower, int upper)
{
// if lower and upper are equal, it means there is ony 1 element
// return true as one character is a palindrome
if (lower == upper)
return true;

// now, check the first and last character and check if they are equal
// if they are not, it can't be a palindrome, return false
if (str[lower] != str[upper])
return false;

// if there are more charcters apart from first and last
// call the recursive function again, and pass the middle indexes
if (upper - lower > 1)
{
// call function again with increasing lower and decreasing upper by 1
return isPalindrome(str, lower + 1, upper - 1);
}
}

// main function
int main()
{
string str; //to store user entered string

// ask user to enter the string
cout << "Enter the string: ";

// loop to ask user and input repeatedly until user enters an empty string by pressing enter key only
while (getline(cin, str) && !str.empty())
{
// check if it is palindrome by calling the function
// for the first call of the function, lower will be starting index, 0
// and upper would be length of str - 1 which is last index
if (isPalindrome(str, 0, str.length() - 1))
{
cout << "It is a Palindrome" << endl;
}
else
{
cout << "It is not a Palindrome" << endl;
}

// ask user again to enter the string
cout << "\nEnter the string: ";
}

return 0;
}

Add a comment
Know the answer?
Add Answer to:
For this assignment, you will create a program that tests a string to see if it...
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
  • C++ A palindrome is a string that reads the same backward as forward. For example, the...

    C++ A palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam and radar are all palindromes. Write a class Pstring that is derived from the STL string class. The Pstring class adds a member functionbool isPalindrome( )that determines whether the string is a palindrome. Include a constructor that takes an STL string object as parameter and passes it to the string base class constructor. Test your class by having a main...

  • C++ A palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam and radar are all palindromes. Write a class Pstring that is derived from the STL string cl...

    C++ A palindrome is a string that reads the same backward as forward. For example, the words mom, dad, madam and radar are all palindromes. Write a class Pstring that is derived from the STL string class. The Pstring class adds a member functionbool isPalindrome( )that determines whether the string is a palindrome. Include a constructor that takes an STL string object as parameter and passes it to the string base class constructor. Test your class by having a main...

  • PLEASE USE MATLAB This is a basic problem that illustrates the concept of strings. A palidrome...

    PLEASE USE MATLAB This is a basic problem that illustrates the concept of strings. A palidrome is a string of characters that is read the same forwards as it is backwards. For example, racecar' is a palindrome; reversing the order of the characters leaves the string unchanged. Write a otherwise. function called isPalindrome that accepts one input and returns one output. The input str is a string of characters, and the output is 1 if str is a palindrome, For...

  • Fix the function so that it checks if the string is a palindrome #include <iostream> using...

    Fix the function so that it checks if the string is a palindrome #include <iostream> using namespace std; //Fix the function so that it checks if the string is a palindrome //(same forwards as backwards ex: racecar / radar) bool is_palindrome(string str, int i){ //base cases if (/* add the condition */) return false;    if (/* add the condition */) return true;    //recursive call return is_palindrome(str, i-1); } int main(){ string x; cin >> x;    if (is_palindrome(x,x.length())){...

  • Your program must prompt the user to enter a string. The program must then test the...

    Your program must prompt the user to enter a string. The program must then test the string entered by the user to determine whether it is a palindrome. A palindrome is a string that reads the same backwards and forwards, such as "radar", "racecar", and "able was I ere I saw elba". It is customary to ignore spaces, punctuation, and capitalization when looking for palindromes. For example, "A man, a plan, a canal. Panama!" is considered to be a palindrome....

  • ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName)...

    ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • Write a function palcheck (char word II) which takes a C-style string word (or a C++...

    Write a function palcheck (char word II) which takes a C-style string word (or a C++ string word if you wish) and returns or false depending on whether or not the string is a palindrome. (A palindrome is a string which reads the same backwards and forwards. Some classic example of palindromes are radar, racecar, toot, deed, bib, civic, redder and madam.) Use your function from part (a) in a complete C++ program which, for each small letter 'a' through...

  • I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not...

    I keep getting an error code in my C++ program. It says "[Error] 'strlen' was not declared in this scope" in my main.cpp. Here are my codes. main.cpp #include <iostream> #include <string> #include "functions.h" using namespace std; //definition of the main function. //takes arguments from the command-line. int main(int argc, char *argv[]) { //Determine if you have enough arguments. //If not, output a usage message and exit program if (argc<2 || (argc == 2 && argv[1][0] == '-')) { //call...

  • Write a python program that contains a hardcoded string and checks whether it is a palindrome...

    Write a python program that contains a hardcoded string and checks whether it is a palindrome (the same forwards as backwards). If it is then display “It is a palindrome”, otherwise display “It is not a palindrome”. A hardcoded string is simply a string defined in your program, for example: my_string = “Hello” Some sample palindromes to use are: A car, a man, a maraca Desserts, I stressed Madam, I’m Adam You will need to strip out any punctuation such...

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