i think im missing something could use a pointer heres the problem
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 function
bool 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.

not looking for the answer just a push in the right direction heres the code I do have.
main.cpp

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

int main(int argc, char * const argv[]){
	string test;
	cout << "Enter word to test: ";
	cin >> test;
	Pstring palindromeTest(test);

	return 0;
}

Pstring.cpp

#include "Pstring.h"

Pstring::Pstring() {
	// TODO Auto-generated constructor stub

}

Pstring::~Pstring() {
	// TODO Auto-generated destructor stub
}

Pstring::Pstring(string word){
	static string testWord = word;
	if (isPalindrome() == true) {
		cout << "the word is a palindrome";
	} else if (isPalindrome() == false) {
		cout << "the word is not a palindrome";
	}
}

bool Pstring::isPalindrome(){
	int i,j;
	string tempString = testWord;
	for (i = 0, j = tempString.length(); i <= j; i++, j--) {
		cerr<<testWord[i];
		cerr<<tempString[j];
		if (testWord[i] != tempString[j]) {
			return false;
		}
	}
	return true;
}

and the .h

#ifndef PSTRING_H_
#define PSTRING_H_
#include <iostream>
#include <string>
using namespace std;
class Pstring : public string {
public:
	Pstring();
	Pstring(string word);
	virtual ~Pstring();
	bool isPalindrome();

private:
	string testWord;
};

#endif /* PSTRING_H_ */

thanks for any help

Recommended Answers

All 4 Replies

You need to be doing something like this in the constructor:

Pstring::Pstring(string word): string(word)
{
	if (isPalindrome() ) 
		cout << "the word is a palindrome";
	 else
		cout << "the word is not a palindrome";
}

The ": string(word)" I added to the constructor will initialise the std::string part of Pstring with the passed in std::string (aka word).
Also you only need to call isPalindrome() once, not twice! And you don't need the static std::string in your constructor either.

Next up, in your isPalindrome function, you can retrieve the string from the base class (the std::string part) of your Pstring by using the c_str() function, which returns a c style string. If you use the c style string to create a tempory local std::string in your function, you can then use your two indexes (i and j) to index the string. No need to use two strings here! Simply set i to index the start of the string and j to index the end and compare the two values before incrementing i, decrementing j and reiterating through the loop.

Like so:

bool Pstring::isPalindrome()
{
	int i,j;
    
    // create a local string (using the std::string.c_str() function to retrieve the word stored in the base class part of Pstring) 
	string testWord(this->c_str());
	for (i = 0, j = testWord.size()-1; i <= j; i++, j--) {
		if (testWord[i] != testWord[j]) {
			return false;
		}
	}
	return true;
}

Note, the index j is initialised to testWord.size()-1 to take the \0 at the end of the string into consideration!

Cheers for now,
Jas.

Alternatively, you could use two std::string::iterators to iterate through the string comparing values like so:

bool Pstring::isPalindrome()
{
    // create a local string (using the std::string.c_str() function to retrieve the word stored in the base class)
    string testWord(this->c_str());

    string::iterator front, back;
    for(front = testWord.begin(), back = testWord.end()-1; front<=back; front++, back--)
    {
        if(*front != *back)
            return false;
    }
    return true;
}

Again, we're initialising back to testWord.end()-1 to avoid the null at the end of the string.

Cheers for now,
Jas.

{Slaps forehead} Doh! Just thought of another thing...
There's no need for the copy of the string if we're using iterators.
We can do this instead:

bool Pstring::isPalindrome()
{
    string::iterator front, back;
    for(front = this->begin(), back = this->end()-1; front<=back; front++, back--)
    {
        if(*front != *back)
            return false;
    }
    return true;
}

J.

Thanks for the help the first way was the easiest got it working was think ing it was a cstring thing

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.