+ 1

why this two strings are not equal

#include <iostream> #include <string> using namespace std; int isPalindrome(string x){ string res; for (int i = x.size(); i >= 0; i--){ res += x[i]; } cout << x.compare(res) << endl; return res==x ? 1 : 0; } int main() { string num; cin >> num; printf("%d",isPalindrome(num)); return 0; } when I enter a string like "8888" the output is that is not a palindrome... but the strings are really equal... could you tell me why my code doesn't work?

13th Mar 2023, 7:02 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
3 odpowiedzi
+ 2
Try,,, x.size() - 1.
13th Mar 2023, 7:29 PM
rodwynnejones
rodwynnejones - avatar
+ 2
Read this ... Returns a signed integral indicating the relation between the strings: value relation between compared string and comparing string 0 They compare equal <0 Either the value of the first character that does not match is lower in the compared string, or all compared characters match but the compared string is shorter. >0 Either the value of the first character that does not match is greater in the compared string, or all compared characters match but the compared string is longer.
13th Mar 2023, 7:34 PM
rodwynnejones
rodwynnejones - avatar
+ 1
14th Mar 2023, 8:01 AM
Ugulberto Sánchez
Ugulberto Sánchez - avatar