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):
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.
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;
}
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.
For each test case, output "Yes" or "No" and the string .