Question

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 program that asks the user to enter a string. The program uses the string to initialize a Pstring object and then calls isPalindrome()to determine whether the string entered is a palindrome.You may find it useful to use the subscript operator [] of the string class: If str is a string object and k is an integer, then str[k] returns the character at position k in the string.

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

Palindrome_Testing.cpp

#include <string>
#include <iostream>
#include "Pstring.h"
using namespace std;


int main()
{
   string test_str;
   //get the input from the user
   cout << "Enter the string to test with no spaces: ";
   getline(cin, test_str);
   system("cls");


   Pstring str(test_str);
   //display the message whether the string is a palindrome
   if (str.isPalindrome())
       cout << "Yes, this string is a palindrome!";
   else
       cout << "No, this string is not a palindrome.";

   cout << endl << endl;
   system("pause");
    return 0;
}


Pstring.cpp

#include <iostream>
#include "Pstring.h"
#include <string>
using namespace std;

/*
This function determines if the string is a palindrome.
It finds the middle in the string and from the middle it goes to
the left and to the right one index at a time and compares chars at
the left and at the ridht from the middle. If they match then the
string is the palindrome.
*/
bool Pstring::isPalindrome()
{
   bool pal = true;     //indicates if string is palindrome
  
   int start_r;         //right index to go to the end of the string
   int start_l;         //left index to go to the begining of the string
  
   //if string has even number of chars
   if (length() % 2 == 0)
   {
       start_r = (this->length()) / 2;          //set right index
       start_l = start_r - 1;                   //set left index
   }
   else
   {
       start_r = ((this->length()) / 2) + 1;        //set right index
       start_l = ((this->length()) / 2) - 1;        //set left index
   }
   //iterate until mismatch found or the end of the string reached
   while (pal && start_r < this->length())
   {
       //if chars at these locations are not equal set pal to false
       if ((*this)[start_l] != (*this)[start_r])
       {
          
           pal = false;
       }
       start_l--;     //decrement left index
       start_r++;     //increment right index
   }
  
   return pal;
}


Pstring.h

#ifndef PSTRING_H
#define PSTRING_H

#include <iostream>
#include <string>
using namespace std;

class Pstring: public string
{
public:
   Pstring(string s) : string(s) { }
   int len() { return this->length(); }
   bool isPalindrome();
private:

};


#endif

Add a comment
Know the answer?
Add Answer to:
C++ A palindrome is a string that reads the same backward as forward. 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
  • 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...

  • A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g.,...

    A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. In this program, ask the user to input some text and print out whether or not that text is a palindrome. Create the Boolean method isPalindrome which determines if a String is a palindrome, which means it is the same forwards and backwards. It should return a boolean of whether or not it was a palindrome. Create the method reverse...

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

  • In C language: A palindrome is a string that reads the same forwards or backwards; for...

    In C language: A palindrome is a string that reads the same forwards or backwards; for example dad, mom, deed (i.e., reversing a palindrome produces the same string ). Write a recursive, bool-valued function, isPalindrome that accepts a string and returns whether the string is a palindrome.

  • A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks...

    A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks and considering uppercase and lowercase versions of the same letter to be equal. For example, the following are palindromes: • warts n straw • radar • Able was I ere I saw Elba • tacocat Write a program that will accept a sequence of characters terminated by a period and will decide whether the string—without the period—is a palindrome. You may assume that the...

  • A Palindrome is a string that is spelled the same way forward and backward (example: radar)....

    A Palindrome is a string that is spelled the same way forward and backward (example: radar). Write a Java program that asks the user to input a string and tests whether the string is a Palindrome or not. Display the message: "The string is a Palindrome" if it is, or "The string is NOT a Palindrome" if it is not. Assume that the user will enter a string without any spaces. The string can be any length. The String can...

  • A Palindrome is a string that is spelled the same way forward and backward (example: radar)....

    A Palindrome is a string that is spelled the same way forward and backward (example: radar). Write a Java program that asks the user to input a string and tests whether the string is a Palindrome or not. Display the message: "The string is a Palindrome" if it is, or "The string is NOT a Palindrome" if it is not. Assume that the user will enter a string without any spaces. The string can be any length. The String can...

  • Problem 1: (Palindromes) A palindrome is a string that's spelled the same way forward and backward....

    Problem 1: (Palindromes) A palindrome is a string that's spelled the same way forward and backward. Some examples of palindromes are; radar, able was i ere i saw elba; and, if you ignore blanks, a man a plan a canal panama .Write a recursive function testPa1indrome that returns 1 if the string stored in the array is a palindrome and 0 otherwise. The function should ignore spaces and punctuation in the string. Please I need it in C program and...

  • A palindrome is a sequence of characters that reads the same backward as forward. For example,...

    A palindrome is a sequence of characters that reads the same backward as forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write an application that reads in a five-digit integer and determines whether it's a palindrome. If the number is not five digits long, display an error message and allow the user to enter a new value.

  • Palindromes A palindrome is a nonempty string over some alphabet that reads the same forward and...

    Palindromes A palindrome is a nonempty string over some alphabet that reads the same forward and backward. Examples of palindromes are all strings of length 1, civic, racecar, noon, and aibohphobia (fear of palindromes). You may assume that in the problems below, an input string is given as an array of characters. For example, input string noon is given as an array s[L.4], where s[1] = n, s[2] = o, s[3] = o, and s[4] = n. (a) (15 pts)...

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