FAQ: Code Challenge: C++ Functions - Palindrome

Hey this is 4 years later but as someone that hasnt done any other languages the replies to this didnt provide any useful information until I did more research so i thought i’d give a more complete answer for anyone else in the future.

   A vector.size() will give you the number of elements in the vector. 

So for example madam.size() would give us 6, (There are 6 char(elements) in the vector/string. Because vectors are indexed at 0, meaning the first m would be index 0, and the last m would be index 5, (1 less than the actual size), we want to start our iterator at the index of the last element.

Previously we have been starting it at 0, which is fine for iterating forward from the beginning, since vectors start at index 0, but since we want to iterate from the last element backwards, we have to take the size of the vector and -1 to get to the index of the last element!

Hope this helps anyone else in the future that may or may not have any experience in other languages.

#include

// Define is_palindrome() here:
bool is_palindrome(std::string text)
{
int len = text.length();
for(int i = 0; i < len; i++)
{
if(text[i] != text[len - 1 - i])
{
return false;
}
}
return true;
}

int main() {

std::cout << is_palindrome(“madam”) << “\n”;
std::cout << is_palindrome(“ada”) << “\n”;
std::cout << is_palindrome(“lovelace”) << “\n”;

}

You don’t have to be rude about it. For a lot of people, this is their first course in any kind of programming language and as others have said, they expected more instruction from the website they’re on; not to have to use google to find another site with the explanation. Also, as it’s some of these people’s first time learning a coding language, its probably not necessarily obvious what they’d even google in the first place. Some people, like myself, get caught up in and need to understand WHY things work the way they do, rather than just seeing it used elsewhere and being able to transfer it to the problem at hand.

codecademy doesn’t like my code, but it works! here it is: (the double minuses and the #include are missing but are there
#include
#include

// Define is_palindrome() here:
std::string is_palindrome (std::string text) {
if (text.size() % 2 == 0) {
int from_left = 0;
int from_right = text.size() - 1;
while (from_left < from_right) {
if (text[from_left] != text[from_right]) {
return “false”;
}
else {
from_left++;
from_right–;
}
}
return “true”;
}
else {
int from_left = 0;
int from_right = text.size() - 1;
while (from_left != from_right) {
if (text[from_left] != text[from_right]) {
return “false”;
}
else {
from_left++;
from_right–;
}
}
return “true”;
}
}

int main() {

std::cout << is_palindrome(“madam”) << “\n”;
std::cout << is_palindrome(“ada”) << “\n”;
std::cout << is_palindrome(“lovelace”) << “\n”;

}

To preserve code formatting in forum posts, see: How do I format code in my posts?

A string and a boolean are not the same thing.

return "true";
return "false";
// is not the same as
return true;
return false;

i had tried that first, it still didn’t work

Post all of your code with proper formatting. And, also share the exact feedback message that pops up at the bottom. That may offer clues as to why your solution is not being accepted.

I figured this one out, mostly on my own but with a little help in this thread as well. My solution is below but I have a question about line 10, which is rev_text += text[size - i - 1];

I initially had this line as rev_text[i] == text[size - i - 1] and I could not figure out why that would not work. After going back through notes I think I realize now why it wouldn’t but would love for a pro to confirm.

Is it because I was treating rev_text as an array and you cannot change the elements of an array?

#include

// Define is_palindrome() here:

bool is_palindrome(std::string text) {
std::string rev_text = “”;
int size = text.size();

for (int i = 0; i < size; i++) {
rev_text += text[size - i - 1];
}
if (rev_text == text) {
return true;
}
else {
return false;
}
}

int main() {

std::cout << is_palindrome(“madam”) << “\n”;
std::cout << is_palindrome(“ada”) << “\n”;
std::cout << is_palindrome(“lovelace”) << “\n”;

}