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.

1 Like

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”;

}

Hello everyone,

Can someone please explain to me why we say (text.size-1) to iterate through the reversed text. I am a beginner.

#include

// Define is_palindrome() here:
bool is_palindrome(std::string text)

{
std::string reveresed_text = “”;

for (int i = text.size()-1; i >= 0; i–)
{reveresed_text += text[i];}

if (reveresed_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”;

}

Thank you

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

In C++, strings and arrays are zero-indexed i.e. the first element is at index 0, the second element is at index 1 and so on.

Consider the string "lovelace".
The size or length of this string is 8 because there are eight characters in the string.
The elements at each index are:

0     1     2     3     4     5     6     7
l     o     v     e     l     a     c     e

There are 8 characters in the string, but the last character is at index 7 (i.e. one less than the size/length of the string).

In the loop,

for (int i = text.size()-1; i >= 0; i--) { ...

we are using the loop variable i to iterate over the indices of the string. If the string is "lovelace", then we want the loop variable to iterate over the sequence 7, 6, 5, 4, 3, 2, 1, 0 so that we can use text[i] to access the characters of the string in reverse order.

Thank you very much for your explanation. This is very helpful.

1 Like

I think there should be more test cases.

For instance, “aa” is certainly palindrome, but when I ran the code below, it said 0 (should say 1, though), and still passed.

#include <iostream>

// Define is_palindrome() here:
bool is_palindrome(std::string text) {
  
  int l = 0;
  int r = text.size() - 1;

  while (l < r) {
    if (text[l] != text[r]) {
      return false;
    } else {
      l++;
      r--;
    }
  }

  return l == r;
}

int main() {
  
  std::cout << is_palindrome("madam") << "\n";
  std::cout << is_palindrome("ada") << "\n";
  std::cout << is_palindrome("lovelace") << "\n";
  std::cout << is_palindrome("aa") << "\n";
  
}

Please correct me if I am wrong.

@net0927061780 You are right that there should be more edge cases added, even for cases like a, but CC lesson writers and checkers typically do not update past lessons, so it is good to leave that as a self-note to be taken.