Palindrome Error

Hi, I've to made a program to check whether a string is a palindrome or not..
I've made following program.. After 2 hours of brainfucking efforts.. I'm still unable to figure out why this is not working correctly..
Thanks in advance to Helping hands
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
34
35
#include <iostream>
#include <string>
using namespace std;
int main ()
{
	string Mystr;
	cout<<"Enter a string: ";
	getline(cin,Mystr);

	cout<<"\nLength is: "<<Mystr.length();

	bool flag= false;	// to set the status of string
						// whether the string is palindrome or not


	cout<<"\n___________________\n";
	for (int k=Mystr.length(); k>=0; k--)
	{
		int temp=0;
		if (Mystr[k]==Mystr[temp])
		{
			flag=true;
		}
		else
			flag=false;
	}
	if (flag==true)
	{
		cout<<Mystr<<" is palindrome\n";
	}
	else
		cout<<Mystr<<" is not palindrome\n";
	cin.ignore();
	return 0;
}
I think k=Mysrt.length() should be

k=(Mysrt.length()-1)
I think k=Mysrt.length() should be

k=(Mysrt.length()-1)

Also doesn't works that way..
wait! you are not changing value of temp... You have to change it (increment by 1) every time
wait! you are not changing value of temp... You have to change it (increment by 1) every time

still not working that way
could you show what you did?
This is what I did..
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
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
using namespace std;
int main ()
{
	string Mystr;
	cout<<"Enter a string: ";
	getline(cin,Mystr);

	cout<<"\nLength is: "<<Mystr.length();

	bool flag= false;	// to set the status of string
				       // whether the string is palindrome or not

	cout<<"\n___________________\n";
	for (int k=Mystr.length()-1; k>=0; k--)
	{
		cout<<"Loop "<<k<<" iteration "<<endl;
		//cout<<Mystr[k]<<endl;
		int temp=0;
		if (Mystr[k]==Mystr[temp])
		{
			flag=true;
			temp++;
		}
		else
		{
			flag=false;
			break;
		}


	}
	if (flag==true)
	{
		cout<<Mystr<<" is palindrome\n";
	}
	else
		cout<<Mystr<<" is not palindrome\n";
	//cin.ignore();
	return 0;
}
int temp=0; should be outside the body of loop....

Otherwise it will always initialize it to zero and we definitely don't want that :D
Thank You Dear..
I can't tell how Happy I am..
Bundle of thanks dear.. Have a blessed long life..
Love you.. Once again Thankss
Welcome :D

why is this not working now

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
using namespace std;

bool isPalindrome (string Mystr);
int main ()
{
	string Mystr;
	cout<<"Enter a string: ";
	getline(cin,Mystr);

	cout<<"\nLength is: "<<Mystr.length();

	bool check;
	check=isPalindrome(Mystr);

	if (check)
		cout<<Mystr<<" is Palindrome\n";
	else
		cout<<Mystr<<" is not Palindrome\n";

	cin.ignore();
	return 0;
}
bool IsPalindrome (string Mystr)
{
	bool flag= false;	// to set the status of string
						// whether the string is palindrome or not
	int temp=0;
		cout<<"\n___________________\n";
		for (int k=Mystr.length()-1; k>=0; k--)
		{
			cout<<"Loop "<<k<<" iteration "<<endl;
			//cout<<Mystr[k]<<endl;
			if (Mystr[k]==Mystr[temp])
			{
				flag=true;
				temp++;
			}
			else
			{
				flag=false;
				break;
			}
		}
		return flag;
}
try keeping Mystr.length-1 within brackets like this (Mystr.length-1)
> why is this not working now
because it doesn't build.
c++ is case-sensitive
Topic archived. No new replies allowed.