the program about palindrome

what wrong with the program?why i can not run!!~~ emergency!!~~~
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
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
	string str1;
	string str2;
	bool pldm=false;
	int size;

	cout<<"enter string: "<<endl;
	getline(cin,str1);
	size=str1.length();
	for(int a=0;a<size;a++)
	{
		if(isupper(str1[a]))
		{
			str1[a]=tolower(str1[a]);
		}
	}

	for(int b=size;b>0;b--)
	{
		str2=str2.append(str1[b],1);
	}

	for(int c=size;c>0;c--)
	{
		if(str1[c]==str2[c])
		{
			pldm=true;
		}
		else
			pldm=false;
	}

	if(pldm)
	{
		cout<<"it is palindroma"<<endl;

	}
	else
	{
		cout<<"it is not palindroma"<<endl;
	}


	system("pause");
	return 0;

}
Last edited on
That's a very unusual algorithm. It might help if you described the algorithm in words, then implement that algorithm once you're convinced it's correct.

If you had a pen and paper, how would you do it?
closed account (G309216C)
Hi DingDong,

Creating a Algorithm for Palindrome is very easy. I am, not going to give a code snippet nor any pointers about this, I will only say read the Book or read the chapter again until you grasp the concept.
Use Pen & Paper first before you start typing away!

Thanks,
SpaceWorm
for(int b=size;b>0;b--)
{

str2=str2.append(str1.[b],1);
}
here is the error
and u cannot use [] for strings
use
namestring.at(b) instead
anyway when u use .at u get out of range fix it for urself
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
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
	string str1;
	string str2;
	bool pldm=false;
	int size;

	cout<<"enter string: "<<endl;
	getline(cin,str1);
	size=str1.length()-1;
	
	for(int a=0;a<size;a++)
	{
		if(isupper(str1.at(a)))
		{
			str1.at(a)=tolower(str1.at(a));
		}
	}
	///////////////
	for(int b=size;b>0;b--)
	{
	
		str2=str2.append(str1.at(b),1);
	}
		////////////////
	for(int c=size;c>0;c--)
	{
		if(str1.at(c)==str2.at(c))
		{
			pldm=true;
		}
		else
			pldm=false;
	}

	if(pldm)
	{
		cout<<"it is palindroma"<<endl;

	}
	else
	{
		cout<<"it is not palindroma"<<endl;
	}


	system("pause");
	return 0;

}

ur code is not good anyway for palindrome
fixed + u need to add -1 to for length
Last edited on
thanks for your cooperation~yeah,i got it!!^.^~i learning C++ just 2 month and i will trying to write a good code as well~ :D
Topic archived. No new replies allowed.