I suck at c++ (palindrome problem)

Pex

Banned
Aug 21, 2003
1,161
0
0
#include <iostream>
#include <string>
using namespace std;

void stringrev(char *n);
string user_input;

int main()
{

string user_input;
int palindrome_test;

cout << "Please input a word to be tested:";
cin >> user_input;
cout << "The word to be tested is: "
<< user_input;

palindrome_test = stringrev(user_input);

if(user_input == stringrev)
{
cout << "The word tested is a palindrome.";
else
cout << "The word tested is not a palindrome.";
}

return 0;
}


void stringrev(temp *n)
{
int i = 0;
int len = strlen(n) - 1;
char temp;


while((len - i) > i)
{
temp = n[len-i];
n[len - i] = n;
n = temp;
i++;
}

cout <<endl;
}








anyone wanna take a crack at it and tell me wtf is wrong..im too tired to think.
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
Your stringrev function is stopping too late, you end up swapping each pair of characters twice.

The loop needs to stop when you reach the middle of the string.

 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Rather than mess with the string reversal, why not just do something like the following?

int isPalindrome(const char *s)
{
char *b = NULL;
char *e = NULL;

if (s == NULL)
return 0;

b = s;
e = &s[strlen(s) - 1];

while (*b != '\0')
if (*b++ != *e--)
return 0;

return 1;
}

I whipped it up in C real quick, and it seems to work; just seems cleaner to me.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
there seem to be a number of things wrong with the original program.

1) i would have expected the comparison to be between user_input and palindrome_test, assuming palindrome_test contains the results from reversing the string
2) string_rev should return a string, not void
3) i don't know what is assigned to palindrome_test since string_rev returns void.

shouldn't the program logic be to call string_rev to create a new string that is the reverse of the user input and then to compare it with the original?

alternately, i think it's reasonable to count off letters at both ends and see if they always match.
 

MrMilney

Senior member
Aug 12, 2000
678
0
0
Here's a little something I whipped up for ya. It's much simpler (to my eye) than what you had originally, and it has the added benefit of actually working. :)

There's plenty of optimization that can be done to it if you are so motivated.


#include <iostream>
using namespace std;

bool testpal(char*);

int main()
{
char user_input[256]; // beware evil magic numbers

cout << "Please input a word to be tested:";
cin >> user_input;
cout << endl << "The word to be tested is: " << user_input << endl;

if(testpal(user_input))
cout << "The word tested is a palindrome." << endl;
else
cout << "The word tested is not a palindrome." << endl;

return 0;
}

bool testpal(char* s)
{
int len = strlen(s);

for(int i=0; i<len; ++i)
{
if(*(s+i) != *(s+len-i-1))
return false;
}

return true;
}
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
char* and string are not the same

try this:

void stringrev(string& s)
{
int a = 0;
int b = s.size() - 1;
char temp;

while (a < b)
{
temp = s[a];
s[a] = s[ b ];
s[ b ] = temp;
++a;
--b;
}
}

this will reverse the string passed to stringrev
 

MrMilney

Senior member
Aug 12, 2000
678
0
0
Originally posted by: dighn
char* and string are not the same

try this:

void stringrev(string& s)
{
int a = 0;
int b = s.size() - 1;
char temp;

while (a < b)
{
temp = s[a];
s[a] = s[ b ];
s[ b ] = temp;
++a;
--b;
}
}

this will reverse the string passed to stringrev
Actually, in C/C++ char* and string are the same thing. Any traditional string is simply an array of chars and any array can be thought of as a pointer to the first element. If you read my code you will see that I am not using a String class but am relying on the actual characteristics of how C/C++ handles strings. Additionally, my method does not require a copy of the string to be made as the actual string is never modified; it's simply examined.
 

EmperorRob

Senior member
Mar 12, 2001
968
0
0
if(user_input == stringrev)
{
cout << "The word tested is a palindrome.";
else
cout << "The word tested is not a palindrome.";
}


??? What's up with your brackets?

should be:

if
{
cout
} else {
cout
}
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: MrMilney
Originally posted by: dighn
char* and string are not the same


this will reverse the string passed to stringrev
Actually, in C/C++ char* and string are the same thing. Any traditional string is simply an array of chars and any array can be thought of as a pointer to the first element. If you read my code you will see that I am not using a String class but am relying on the actual characteristics of how C/C++ handles strings. Additionally, my method does not require a copy of the string to be made as the actual string is never modified; it's simply examined.

I was referring to the program by the original poster. in his program he is using the standard library class std::string which does not have a conversion operator to char* and he passes it to a function that takes a char*.

Your method is good but I didn't want to change too much from his existing program.

 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
The cheesy way:

string palindrome_test = user_input;
strrev((char *)palindrome_test.c_str());

if(user_input == palindrome_test)
{
cout << "The word tested is a palindrome.";
else
cout << "The word tested is not a palindrome.";
}
 

Pex

Banned
Aug 21, 2003
1,161
0
0
Originally posted by: MrMilney
Here's a little something I whipped up for ya. It's much simpler (to my eye) than what you had originally, and it has the added benefit of actually working. :)

There's plenty of optimization that can be done to it if you are so motivated.


#include <iostream>
using namespace std;

bool testpal(char*);

int main()
{
char user_input[256]; // beware evil magic numbers

cout << "Please input a word to be tested:";
cin >> user_input;
cout << endl << "The word to be tested is: " << user_input << endl;

if(testpal(user_input))
cout << "The word tested is a palindrome." << endl;
else
cout << "The word tested is not a palindrome." << endl;

return 0;
}

bool testpal(char* s)
{
int len = strlen(s);

for(int i=0; i<len; ++i)
{
if(*(s+i) != *(s+len-i-1))
return false;
}

return true;
}




could you expain to me how that works? mainly the function.
 

Krakerjak

Senior member
Jul 23, 2001
767
0
0
Originally posted by: Pex
Originally posted by: MrMilney

bool testpal(char* s)
{
int len = strlen(s);

for(int i=0; i<len; ++i)
{
if(*(s+i) != *(s+len-i-1))
return false;
}

return true;
}


could you expain to me how that works? mainly the function.

So you have "len" set to the number of characters in the passed string (ie. RACECAR)....so 7
"i" is initialized and pre-incremented so it is now = 1.
*(s+i) would then point to the first character in the string (ie. R)
*(s+len-i-1) would point to the last character in the string (ie. R)
the IF statement is then false so the FOR loop takes over and tests each character for equality.
Will return false as soon as an inequality is found or....
Will return true as soon as "i" has been incremented through to being equal to strlen (ie. whole string has been tested)
 

MrMilney

Senior member
Aug 12, 2000
678
0
0
Originally posted by: Krakerjak

So you have "len" set to the number of characters in the passed string (ie. RACECAR)....so 7
"i" is initialized and pre-incremented so it is now = 1.
*(s+i) would then point to the first character in the string (ie. R)
*(s+len-i-1) would point to the last character in the string (ie. R)
the IF statement is then false so the FOR loop takes over and tests each character for equality.
Will return false as soon as an inequality is found or....
Will return true as soon as "i" has been incremented through to being equal to strlen (ie. whole string has been tested)
Actually, that's not entirely correct. i is not incremented until after the loop has executed so on the first pass i = 0. This is good, because arrays in C/C++ are zero-based. This means that *(s+i) is the same as *(s) which returns the first element of the array. The rest is 100% correct including the fact that I was lazy and didn't stop testing the string at the halfway point.

:)
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: MrMilney
Originally posted by: dighn
char* and string are not the same

try this:

void stringrev(string& s)
{
int a = 0;
int b = s.size() - 1;
char temp;

while (a < b)
{
temp = s[a];
s[a] = s[ b ];
s[ b ] = temp;
++a;
--b;
}
}

this will reverse the string passed to stringrev
Actually, in C/C++ char* and string are the same thing. Any traditional string is simply an array of chars and any array can be thought of as a pointer to the first element. If you read my code you will see that I am not using a String class but am relying on the actual characteristics of how C/C++ handles strings. Additionally, my method does not require a copy of the string to be made as the actual string is never modified; it's simply examined.

char * and "string" in C/C++ are not the same, as C/C++ isn't a language, and C doesn't have any concept of strings.
 

MrMilney

Senior member
Aug 12, 2000
678
0
0
Originally posted by: Descartes
Originally posted by: MrMilney
Originally posted by: dighn
char* and string are not the same

try this:

void stringrev(string& s)
{
int a = 0;
int b = s.size() - 1;
char temp;

while (a < b)
{
temp = s[a];
s[a] = s[ b ];
s[ b ] = temp;
++a;
--b;
}
}

this will reverse the string passed to stringrev
Actually, in C/C++ char* and string are the same thing. Any traditional string is simply an array of chars and any array can be thought of as a pointer to the first element. If you read my code you will see that I am not using a String class but am relying on the actual characteristics of how C/C++ handles strings. Additionally, my method does not require a copy of the string to be made as the actual string is never modified; it's simply examined.

char * and "string" in C/C++ are not the same, as C/C++ isn't a language, and C doesn't have any concept of strings.
Which is what I said. C and C++ do not have a built-in string type. Instead they use arrays of chars. Any array name in C or C++ can be thought of as a pointer to the first element in the array. See K&R if you have any questions.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Originally posted by: MrMilney
Originally posted by: Descartes
Originally posted by: MrMilney
Originally posted by: dighn
char* and string are not the same

try this:

void stringrev(string& s)
{
int a = 0;
int b = s.size() - 1;
char temp;

while (a < b)
{
temp = s[a];
s[a] = s[ b ];
s[ b ] = temp;
++a;
--b;
}
}

this will reverse the string passed to stringrev
Actually, in C/C++ char* and string are the same thing. Any traditional string is simply an array of chars and any array can be thought of as a pointer to the first element. If you read my code you will see that I am not using a String class but am relying on the actual characteristics of how C/C++ handles strings. Additionally, my method does not require a copy of the string to be made as the actual string is never modified; it's simply examined.

char * and "string" in C/C++ are not the same, as C/C++ isn't a language, and C doesn't have any concept of strings.
Which is what I said. C and C++ do not have a built-in string type. Instead they use arrays of chars. Any array name in C or C++ can be thought of as a pointer to the first element in the array. See K&R if you have any questions.

You seem to glom C and C++ together. I was just pointing that C++ *does* have a string type (ANSI C++) and C does not, regardless of how it's treated underneath (an array of chars).