I’m getting acquainted with c++ but don’t know how to compare indexed characters from the same or different strings. Here’s a palindrome example that takes an int and converts it to a string through a stringstream.
bool ispalindrome(int a) {
stringstream stream;
stream<<a;
string str = stream.str();
int length = str.length();
int offset = length - 1;
for (int i=0; i<=offset; i++ && offset--) {
if (str[i] == str[i + offset]) {
return false;
}
offset--;
}
return true;
}
For some reason this is always evaluated to false. I wouldn’t think null termination would have anything to do with it because it’s not reported by the length, so I guess I must be using the wrong comparison method. I can’t seem to find something like strncmp but with single characters.
[Edit: fixed title]
I don’t know how you wrote that loop, but I’m pretty sure it should be