boolean palandrome

This code always return to true, how can i fix it? and what's the problem?
#include <string>
#include <iostream>

using namespace std;

bool Function(std::string str)
{//this function remove punctuation and space.


string String;

for (int i=0, j=0;i<str.length();i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
String.append(1,str[i] + 0x20); //make Lowercase by adding 0x20
}
else if (str[i] >= 'a' && str[i] <= 'z')
{
String.append(1,str[i]);
}

if(i>=j)
return true;
if(str[i] == str[j])
return Function(str.substr(i+1,j-i-1));
else
return false;

return(true);

}
}
int main()
{
string strIn;

cout<<"please enter a word phrase\n";
getline(cin,strIn);

if (Function(strIn))
cout<<"it is a palindrome"<<endl;
else
cout<<"it is not a palindrome"<<endl;
}
Since if(i>=j) is always true (j is always 0) so it will always return true

Hi again,

Ive altered the code so that function is recursive

and also put the convert to lowercase and remove punctuation and spaces
into a seperate function.

Please Try to limit yourself to one thread per subject.

heres the modified 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <string>
#include <iostream>

using	namespace std;

string	convert(string str)
{
	string	tempString;

	for (int i=0, j=0;i<str.length();i++)
	{
		if (str[i] >= 'A' && str[i] <= 'Z')
		{
			tempString.append(1,str[i] + 0x20);
		}
		else if (str[i] >= 'a' && str[i] <= 'z')
		{
			tempString.append(1,str[i]);
		}
	}
	return(tempString);
}

bool Function(std::string str)
{
	int	len = str.length();

	if (len <= 1)
	{
		return(true);
	}

	if (str[0] != str[len-1])
	{
		return(false);
	}
	else
	{
		return(Function(str.substr(1,len-2)));
	}
	

	
}
int main()
{
	string	strIn;

	cout<<"please enter a word phrase\n";
	getline(cin,strIn);

	strIn = convert(strIn);

	if (Function(strIn)) 
		cout<<"it is a palindrome";
	else
		cout<<"it is not a palindrome";
}
Topic archived. No new replies allowed.