I'm learning C++ out of curiosity - my current task is to check if a string is a palindrome. What should be happening is the string is reversed, and I use the Equal To (==) operator to compare the original and reversed string. But for some reason, they're all returning as false, and I can't understand why.
#include <iostream>
using namespace std;
string reverse_string(string text) {
int string_size = text.size();
string reversed = "";
while (string_size >= 0) {
reversed += text[string_size];
string_size--;
};
return reversed;
}
bool is_palindrome(string text) {
if (reverse_string(text) == text) {
return true;
};
return false;
}
int main() {
cout << is_palindrome("madam") << "\n";
cout << is_palindrome("ada") << "\n";
cout << is_palindrome("lovelace") << "\n";
}
For the record, I'm doing this via Codeacademy - It's returning false (0) for both the browser CMD prompt, as well as local.
Something I'm noticing that if I try to log the comparison (reversed_string == text), I'm getting a single random integer. I'm not sure why that's happening; shouldn't it just be returning 0 or 1?