11421 - Palindrome   

Description

Palindrome is a string that is identical to its reverse, like "level" or "aba". Check whether a given string is a palindrome or not. 

Create a class SimpleString that manipulates a given string of characters. This class contains proper constructor functions (default constructor and copy constructor) and supports the following operations (in the form of member functions):

  • Ÿ   Overloading the assignment operator (=) to assign one given SimpleString object to the current SimpleStringobject.
  • Ÿ   Reversing the string which is stored in the SimpleString object.
  • Ÿ   Overloading the relational operator (==) to check whether two strings, which are stored in two SimpleStringobjects, are the same or not.
  • Ÿ   Overloading the operator (>>) to input the string into the SimpleString object.
  • Ÿ   Overloading the operator (<<) to ouput the string from the SimpleString object.

To check if a given string is a palindrome, we have provided another class PalindromeChecker as follows:

class PalindromeChecker

{

public:

    PalindromeChecker(const SimpleString &s)

    {

        str = s;

        rev = s;

        rev.reverse();

    };

    void isPalindrome()

    {

        if(rev == str)

            std::cout << "Yes\n";

        else

            std::cout << "No\n";

    };

private:

    SimpleString str;

    SimpleString rev;

};

Note:

This problem involves three files.

  •   function.h: Class definition of SimpleString.
  •   function.cpp: Member-function definitions of SimpleString.
  •   main.cpp: A driver program to test your class implementation.

You will be provided with main.cpp and function.h, and asked to implement function.cpp.

Note that you don’t need to implement class PalindromeChecker

function.h

#ifndef SIMPLESTRING_H
#define SIMPLESTRING_H
#include <iostream>
using namespace std;

class SimpleString
{
    friend std::ostream &operator<<(std::ostream &, const SimpleString &);
    friend std::istream &operator>>(std::istream &, SimpleString &);
public:
    SimpleString():size(0),ptr(NULL){} // default constructor
    SimpleString( const SimpleString & ); // copy constructor
    ~SimpleString(); // destructor
    const SimpleString &operator=( const SimpleString & ); // assignment operator
    bool operator==( const SimpleString & ) const; // equality operator
    void reverse(); // reverse the string
private:
    size_t size; // pointer-based string size
    char *ptr; // pointer to first element of pointer-based string
}; // end class SimpleString
class PalindromeChecker
{
public:
    PalindromeChecker(const SimpleString &s)
    {
        str = s;
        rev = s;
        rev.reverse();
    };
    void isPalindrome()
    {
        if(rev == str)
            std::cout << "Yes\n";
        else
            std::cout << "No\n";
    };
private:
    SimpleString str;
    SimpleString rev;
};

#endif

main.cpp

#include <iostream>
#include "function.h"

using namespace std;

int main()
{
    SimpleString s;
    while(cin>>s){
        if(cin.eof())break;

        PalindromeChecker c(s);
        c.isPalindrome();
        cout<<s<<endl;
    }

    return 0;
}

Input

The input consists of multiple lines. Each line contains a string. The length of each string is less than 100000. The number of test case is less than 1000. 

Output

For each test case, output "Yes" or "No" and the string . 

Sample Input  Download

Sample Output  Download

Partial Judge Code

11421.cpp

Partial Judge Header

11421.h

Tags




Discuss