Question

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 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 words mom, dad, madam and radar are all palindromes. Write a class Pstring that is derived from the STL string cl...
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...

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

  • 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 any word, phrase, or sentence that reads the same forward and backward. Here...

    A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for the user input....

  • Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward...

    Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for...

  • Please Code in Java and follow the directions given Thanks Program required Directions .A palindrome is a str...

    Please Code in Java and follow the directions given Thanks Program required Directions .A palindrome is a string that reads the same forward and backward, i.e., the letters are the same whether you read them from right to left or from left to right. Examples: 3 a) radar à is a palindrome b)Able was I ere I saw Elba à is a palindrome e good à not a palindrome Write java program to read a line of text and tell...

  • Stack help. I need help with my lab assignment. Complete a method for a class named...

    Stack help. I need help with my lab assignment. Complete a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored. The...

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