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