Palindrome

I am pretty close to being on track with this program, but I have been getting stuck on an area. The program formulas may not be correct, but at the moment all I am doing is just to see how the program works. Here are my codes:

Palindrome.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<string>
using namespace std;

class PString:public string
{
	private: 
		string testWord;
		
	public:
		PString();
		PString(string Pword){setPword(Pword);}
		void setPword(string Pword){ testWord = Pword;}
		bool isPalindrome(){return;};//What do I return here because              
                                             //when I type "return true;" then
                                             //the output would be [word] is a
                                             //palindrome. So I know it's    
                                             //working...or so I think it is...
};


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Chapter 11 - Assignment 5, Palindrome Testing
// This program uses a class derived from string to 
// to test a strings to see if they are palindromes.

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


bool isPalindrome(string strg)
{
	
	
	for (int count = 0; count < strg.length()-1; count++) 
	{
		if (tolower(strg[count]) !=
			tolower(strg[strg.length()-(count+1)]))
		{ 
			return false;
		}
		else 
		{return true;}
	}
	
}

int main ()
{
    // Request input from user
    string str;
    cout << "This is a palindrome-testing program. Enter a string to test:\n";
    cin >> str;

    // Create a PString object that will check strings
    PString s(str);

    // Check string and print output
    if (s.isPalindrome())
        cout << s << " is a palindrome";
    else
        cout << s << " is not a palindrome";
    cout << endl;
    return 0;
}

Also could someone simply explain the reason this particular program should be derived from the string class? what does it do or what's the use of it? Thank you all in advance as well for you time... I'm still learning all this stuff. :)
If you're deriving from the string class you have everything that the string class has. Therefore you don't create a new instance of the string class in your class.

Simply make a couple methods to check on your palindrome. you can use the this pointer.
So you want code that works out if a word is a palindrome?

Ok well a palindrome is simply where
x = y-x

where x is the current letter and y is the number of letters in the string.
This has to be true for every value of x which is less than the amount of letters in the string.
This is a big hint.


Note: The following is untested code but you should get the gist...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool isPalindrome()
{
	bool isAPalindrome = true; //set it to true initially

	//check it has letters in it
	if (testWord.size() > 0)
	{
		for(int n = 0; n < testWord.size(); n++) 
		{
			if (testWord.at(n) != testWord.at(testWord.size()-n-1)) //check if x != y-x
			{
				isAPalindrome = false; //set it to say it is not a palindrome if this is not true
			}
		}
	}

	return isAPalindrome;
}


I think this will work
Last edited on
@ultifinitus: Thanks it makes sense now. Just the this pointer I would have to study a bit more before I use it.

@Chazzmundo: Now, would the code go in palindrome.h or main.cpp? I haven't really test the my code either because in the class, bool palindrome is suppose to return something yet if I leave it blank (ex) bool isPalindrome();, then I would get a unresolved link error. So before using your code I wanted to know what do I return? Thanks.
@Chazzmundo
1
2
3
// Chapter 11 - Assignment 5, Palindrome Testing
// This program uses a class derived from string to 
// to test a strings to see if they are palindromes. 


Notice the derived from string part.
@ultifinitus
Oh I see.
Whoah, disordered posts. Anyways, maybe that's not part of the assignment, I had just assumed...
Topic archived. No new replies allowed.