PDA

View Full Version : Need help with palindrome program (C++)


akebono
01-11-2007, 02:44 AM
Hi i need help with a palindrome program i am writing. Its supposed to have these requirements:
1. correctly determine if a string input by the user is a palindrome.
2. include sample output. Use the input shown above as a guide.
3. ignore all spaces, punctuation marks, apostrophes, and other non-alphanumeric characters.
4. consider lower and upper case letters the same. (i.e. is NOT case sensitive)
5. have one function bool Palindrome (string sText) that tests a string to see if it is a palindrome.
6. The Palindrome function will not modify the original string input by the user

I think i've got the basic palindrome thing to work, but i have no idea how to make the palindrome program ignore the spaces and punctuation marks..
Here's what i've got:

#include <iostream.h>
#include <stdlib.h>
#include <ctype.h>
#include <string>
using namespace std;
bool Palindrome(string sString);
string sString;

int main()
{
cout<<"Welcome to the Palindrome checker."<<endl;
cout<<"This program tests a line of input to see if it is a Palindrome."<<endl;
cout<<"A Palindrome is a word or phrase that reads the same forwards and backwards."<<endl;
cout<<endl;
do
{
cout<<"Enter word to check (or 'quit' to exit): ";
getline(cin, sString);
if(Palindrome(sString))
{
cout<<sString<<" IS a Palindrome"<<endl;
}
else
{
cout<<sString<<" is NOT a Palindrome."<<endl;
}
}while(sString != "quit");
system("PAUSE");
return 0;
}

bool Palindrome(string sString)
{
int nIndex = 0;
do
{
nIndex++;
if(sString[nIndex] == sString[sString.length()-(nIndex + 1)])
{
return true;
}
else
{
return false;
}
}while(sString[nIndex] == sString[sString.length()-(nIndex + 1)]);

}

Thanks for helping..

rpgfan3233
01-11-2007, 03:13 AM
The only way to ignore the spaces and punctuation marks is to create a new string with anything other than alphabetic characters removed from the input string. Reverse the new string and compare the new string to its reversed counterpart. That is the easiest way to do it as far as I know.

Gox
01-11-2007, 04:16 PM
There are various string methods that can help you decide whether a character in a string is alpha-numeric or not. The following will probably be helpful to you.

isalpha(char c) will return true if the character c is alpha.
isdigit(char c) will return true if the character c is a digit.
isspace(char c) will return true if the character is white space (i.e. single-space, tab-character etc.).

I think these methods are in ctype. I notice that you have #include <ctype.h> at the top of your file. The convention for c++ is to remove the .h and just have #include <ctype>, although I don't think it makes any actual difference.

Here is an example of a method I wrote in c++ to strip out any characters from a string that weren't alpha characters (I allowed numbers to remain I believe so you'll need to check for digits as well). You won't be able to just copy and paste it line for line since it was part of a larger project (i.e the methods getStringData(), dataSize(), and alphaData() are inherent to my program), but the general algorithm should be the same for your assignment.

/**
* A method to strip out any characters in the file contents which aren't of
* type "alpha".
* Note: The Alpha data is not set when a new Readfile is constructed. If the
* user wishes to make an alpha version of the file data, this method must be
* called.
**/
void Readfile::setAlphaData()
{
char c;

//Retrieve the string for which we remove all non-alpha characters
const string& data = this->getStringData();

//Remove all non-alpha characters
for(unsigned int i = 0; i < this->dataSize(); i++)
{
//Traverse each character in the string (i.e. data[i]) and make sure it is alpha
c = data[i];
if(isalpha(c) && c != '\n' && !isspace(c))
{
//If the character is alpha add it to the new string
//this->alphaData() returns a string which is a copy of the original string which will only have alpha characters.
this->alphaData().push_back(c);
}
}
}

If you have any other questions feel free to ask.

EDIT: Added to comments to the code to make it easier to read.

EDIT2: Here's the definition of push_back just to make it clear why I used that method to append a character to a string

void push_back(charT c) Append a single character to *this.
A quick example that's a little clearer is as follows

string myString = "Go";
char myChar = 'x';

myString.push_back(c);

//This should result in myString now being Gox

rpgfan3233
01-11-2007, 06:24 PM
Actually, it is #include <cctype> ;-)

Gox
01-11-2007, 07:30 PM
Oops, my mistake. Thanks for the correction. :)

akebono
01-12-2007, 12:36 AM
umm, I don't think i'm supposed to do stuff i haven't learned yet like the isspace and other complicated language..
and I don't really understand what gox is saying in the code..
I'm only in computer programming 1 so its my first few months of learning it,
could you type out just the code that ignores the spaces and punctuation marks?

the website that I'm supposed to do this from is http://www.mrsimon.net and this is assignment 14
p.s. i don't think i learned what rpgfan is saying yet..so i cant do that

rpgfan3233
01-12-2007, 01:07 AM
Actually, you have learned what I mentioned:

// Pseudocode
create a new string
check each individual character in the user's input string
character is a letter A-Z (assuming we are ignoring the case):
concatenate that character at the end of the new string you created
character is not a letter A-Z:
do nothing
reverse the string you created by adding characters to the end and check to see if the reversed string is the same as the string you created
strings are the same:
say the string the user inputted is a palindrome
strings are not the same:
say the string the user inputted is not a palindrome

darkblitz05
02-10-2007, 09:45 AM
can anybody know how to code a palindrome program in C not C++ and must not using strrev?.....

post your reply.. ty

Gox
02-13-2007, 09:40 PM
can anybody know how to code a palindrome program in C not C++ and must not using strrev?.....

post your reply.. ty
If you were to ask me to write a palindrome program without using strrev, then the first thing I would do is essentially write my own code that would be very similar to strrev. So the question I have is, are you not allow to use strrev or are you not allowed to use a technique which would make strrev useful? i.e. Reverse the string.

This problem can be solved easily by taking a word, reversing it and checking that the two words are equal.

marek_mar
02-13-2007, 10:45 PM
My God you complicate matters.

#include <iostream>
#include <cctype>

bool is_palindrome(const char* s)
{
int i = 0;
int j = strlen(s) - 1;
while(j >= 0)
{
if(!isalpha(s[i]))
{
++i;
}
else if(!isalpha(s[j]))
{
--j;
}
else if(tolower(s[i]) != tolower(s[j]))
{
return false;
}
++i;
--j;
}
return true;
}


void printme(const char* s)
{
using std::cout;
using std::endl;
cout << "\"" << s << "\"";
if(is_palindrome(s))
{
cout << " IS a palindrome!" << endl;
}
else
{
cout << " is NOT a palindrome!" << endl;
}
}

int main()
{
char s[] = "kajak";
char s2[] = "Poor Dan is in a droop";
char s3[] = "not a palindrome";

printme(s);
printme(s2);
printme(s3);
return 0;
}

The only C++ things in there is the cout/endl/std.

Gox
02-14-2007, 01:20 AM
Congrats on doing his homework!

While your implementation works for most cases, and does ignore non-alpha characters, the method in which it ignores will cause it to incorrectly identify words that are palindromes (if non-alpha's are ignored) as not palindromes.

marek_mar
02-14-2007, 02:25 AM
Why thank you. (1.5 Rules) (http://www.codingforums.com/rules.htm)

I don't really get the second sentance. He said all non alphanumeric characters should be ignored. Can you give an example?
I myself, am surprised it works at all.

Gox
02-14-2007, 04:12 AM
Agreed, my second sentence wasn't very clear. kaj!jak is a palindrome if we ignore the !... kajjak. Your method wiil report this as not a palindrome.

I am confused by your first sentence... You do his homework and then point to the rule that states we won't do your homework...

marek_mar
02-14-2007, 10:10 AM
Ah. That little error snuck in when I rewrote that function to not use continue.