J.Doe J.Doe - 2 months ago 9
C++ Question

Comparing two string c++

I wanted to create a program to check if strings are Palindromes.Here is my code

#include <iostream>
using namespace std;
int main()
{
string a,b="";
cin>>a;
for (int i = a.size(); i>0; --i)
{
b=b+a[i];
}
if(b==a)
cout<<"Palindrome"<<endl;
else
cout<<"Not Palindrome"<<endl;
return 0;
}


But it always returns "Not palindrome" even when it is.Then i tried using
compare
function instead of
==
but it always returned
-1

Answer

Just change the line like that

for (int i = a.size()-1; i>=0; --i)

Comments