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?
You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.
Use the strict inequality (! ==) operator to check if two strings are not equal to one another, e.g. a !== b . The strict inequality operator returns true if the strings are not equal and false otherwise.
Python strings equality can be checked using == operator or __eq__() function. Python strings are case sensitive, so these equality check methods are also case sensitive.
In String, the == operator is used to comparing the reference of the given strings, depending on if they are referring to the same objects. When you compare two strings using == operator, it will return true if the string variables are pointing toward the same java object. Otherwise, it will return false .
The other answers explain how you've failed to reverse your string; but you don't need to copy it: std::string
has a reverse view built in:
bool is_palindrome(const std::string & text) {
return std::equal(text.begin(), text.end(), text.rbegin());
}
Or, if you want a string that is the reversed, you can construct it from the pair of iterators:
std::string reversed(const std::string & text) {
return { text.rbegin(), text.rend() };
}
C++ indexing is zero-based. Thus string_size
, while initially being the length of the string, is one past the end of the string.
So in your loop’s first iteration you are indexing past the string, leading to the zero termination character ('\0'
) being returned. To fix this it’s enough to decrement the index just before performing the indexing operation.
The index of the last character in a string is that string's size (or length) minus one (because array and other container indexes in C++ are zero-based). So, in your reverse_string
function you need to decrement the index before copying it. You can do this conveniently with the pre-decrement --x
operator, as shown below:
string reverse_string(string text)
{
size_t string_size = text.size(); // Best to use size_t type.
string reversed = "";
while (string_size > 0) {
reversed += text[--string_size]; // Decrement index BEFORE copying
} // No need for a semicolon here!
return reversed;
}
Note also the other minor changes I have made to your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With