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