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>
usingnamespace 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...
};
// 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"
usingnamespace std;
bool isPalindrome(string strg)
{
for (int count = 0; count < strg.length()-1; count++)
{
if (tolower(strg[count]) !=
tolower(strg[strg.length()-(count+1)]))
{
returnfalse;
}
else
{returntrue;}
}
}
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;
}
@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.
// 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.