-
Exercise on Strings
Hello everyone,
i have to program an C++ program that will determine whether a given string is a palindrome or not.(A palindrome is a word that read same forward and backward...like "MADAM")
Here is my program, but i got some error and i don't know why?
Please help.
-
You have a lot of errors in that program syntactic and logical. Here's the fixed program :-)
Code:
#include <iostream.h>
#include <string.h>
int main()
{
int length;
int arr1=0;
typedef char string[30];
typedef char string2[30];
string word[10];
string2 invWord[10];
cout<<"Enter word"<<endl;
cin>>word[arr1];
while (stricmp(word[arr1], "Quit")!= 0)
{
length=strlen(word[arr1]);
for (int j=0, i=length-1; j<length; j++, i--)
{
invWord[arr1][j]=word[arr1][i];
}
invWord[arr1][length] = '\0';
if (strcmp(word[arr1], invWord[arr1]) == 0)
cout<<"Palindrome"<<endl;
cout<<"Enter word"<<endl;
cin>>word[++arr1];
}
return 0;
}
But you can do a lot better, here's my stab at it :-)
Code:
#include <functional>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
class good_char: public unary_function<char, bool> {
const string good;
public:
good_char()
: good("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
{}
bool operator()(const char c) const
{
if (good.find(c) != string::npos)
{
return true;
}
return false;
}
};
bool is_palindrome(string& s)
{
string::iterator new_end = remove_if(s.begin(), s.end(), not1(good_char()));
s.erase(new_end, s.end());
string r = s;
reverse(r.begin(), r.end());
if (s == r)
{
return true;
}
return false;
}
int main()
{
string s;
cout<<"Enter a word or phrase: ";
getline(cin, s);
if (is_palindrome(s))
{
cout<<"You entered a palindrome"<<endl;
}
}
-
hey.
hey cela,
just wanted to make things clear in my head . What does these do?
Code:
class good_char: public unary_function<char, bool> {
const string good;
public:
good_char()
: good(" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX
YZ")
{}
bool operator()(const char c) const
{
if (good.find(c) != string::npos)
{
return true;
}
return false;
}
};
-
>>What does these do?
It's a function object that checks a character given to it with a list of valid characters. If it doesn't match one of those characters then it returns false and remove_if() deletes the character, just what we want. :-)
-
and hey cela ,
sorry again me here. what does this do??
Code:
string::npos
and lastly why is this empty function?
good_char()
: good(" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX
YZ")
{//why is it empty here?}
-
Re: string
hi cela,
unfortunately, i didn't start the true 2 dimensional array yet("array [] []").
So, can you change the program without using the "real" 2 dimensional array?
-
Code:
#include <iostream.h>
#include <string.h>
int main()
{
int length;
typedef char string[30];
typedef char string2[30];
string word;
string2 invWord;
cout<<"Enter word"<<endl;
cin>>word;
while (stricmp(word, "Quit")!= 0)
{
length=strlen(word);
for (int j=0, i=length-1; j<length; j++, i--)
{
invWord[j]=word[i];
}
invWord[length] = '\0';
if (strcmp(word, invWord) == 0)
cout<<"Palindrome"<<endl;
cout<<"Enter word"<<endl;
cin>>word;
}
return 0;
}
-
Re: String
Thanks Cela,
but i don't understand this line:
invWord[length] = '\0'
Without this line, the program doesn't work, why?
if "invWord" need this line, how come "word" don't need this line?
Thanks
-
> invWord[length] = '\0'
This puts a null terminator at the end of the string - it tells the computer the string's done. word doesn't need one because it has one appended to it when it's read in in the cin statement.
-
Just wanted to toss in my 2 cents. Obviously it's going to have a lot less overhead as I've excluded any use of the stl, and it's only 4 lines long ;)
Code:
bool isPalindrome(const char *str) {
for (int l = 0, r = strlen(str) - 1; l < r; ++l, --r)
if (str[l] != str[r])
return false;
return true;
}
-
>>Obviously it's going to have a lot less overhead as I've excluded any use of the stl, and it's only 4 lines long
It also doesn't have much flexibility. Since you're making an exact comparison the following palindrome returns false
Code:
a man, a plan, a canal, panama
-
:)
Oh, yeah. You're right. I didn't take that into consideration. How about this then:
Code:
bool isPalindrome(const char *str) {
for (int l = 0, r = strlen(str) - 1; l < r; ++l, --r) {
while (!isalpha(str[l]))
++l;
while (!isalpha(str[r]))
--r;
if (l < r && toupper(str[l]) != toupper(str[r]))
return false;
}
return true;
}