palindrome.help!


Hi,
this code should check if the string is a palindrome or not. the problem is that it gives me "Is Not Palindrome" always even if it's a palindrome!!
what's wrong with my code? :!

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
#include<iostream>
#include<string>

using namespace std;

int main()
{

	string string1;
	string x;
	

	cout << "Enter the string please: " << endl;
	getline(cin,string1);

	for(int i=string1.length();i>=0;i--)
	{
		cout << string1.substr(i,1);
		x = string1.substr(i,1);
														
	}


      cout << endl;
		if(x == string1)
			cout << "Is a palindrome" << endl;

		else
			cout << "Is Not palindrome" << endl;
	

	return 0;
}
I made a function that did the same thing a while back if you want me to find it and share it I can :)

If I remember right I just used a for loop that started at the end of the string and copied every element into a second string so the characters would be reversed. then i just used strcmp() to see if the two were identical. if they were, then it's a palindrome :)
See the value of x

I would appreciate it if you do. :>

yes I've done with the reverse part it works correctly but , am stuck in how to compare between the
two strings !! . because it always gives me "Is Not palindrome" :/

what is in value of x ?
why not just read it in as a string then use the reverse function int the string class
what is in value of x ?
Exactly. x is suppose to hold the reverse of the string.
That is not happening. Output x and you will see what the problem is.

yes it doesn't hold the reverse:/ . then what to do?

Is cout << string1.substr(i,1); wrong?
x = string1.substr(i,1);
if you output this with a string lets say "palindrome" it just shows the last letter. 'e'.
Topic archived. No new replies allowed.