FAQ: Code Challenge: C++ Functions - Palindrome

This community-built FAQ covers the “Palindrome” exercise from the lesson “Code Challenge: C++ Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise Palindrome

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I don’t really understand how this exercise can help detect a palindrome, is there a step-by-step explanation for each line of code starting from “for”?

6 Likes

The Following is an alternative that the system wouldn’t accept. Not like I expected it to.



#include

// Define is_palindrome() here:
std::string is_palindrome(std::string text) {
int n = text.size() -1 ;
for (int j = 0; j < n; j++ , n–) {
if (text[j] != text[n]) {
return “false”;
}

}
return “true”;
}

int main() {

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

I know it told me to use a bool type function but that way it would return 1 or 0 so…

1 Like

This challange would’ve been more useful had we been introduced to a single string command.

5 Likes

Here’s how I was able to complete the challenge. It took me longer to figure out than I’d like to admit but I guess that’s part of learning. :+1:

#include <iostream>
#include <string>

// Define is_palindrome() here:
bool is_palindrome(std::string text) {
  std::string reversed_text;
  
  for (int i = text.size() - 1; i >= 0; i--) {
    reversed_text.push_back(text[i]);
  }

  if (reversed_text != text) {
    return false;
  } else {
    return true;
  }
}

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

I agree, I feel as though it wasn’t made clear at any point before or during the exercise. There should be optional practice exercises offered for honing purposes.

3 Likes

Hi!

I have a quick question about your code. Why would you initialize i as text.size() - 1? Why doesn’t initializing i as text.size() work?

Thank you so much!

I don’t think there is a problem with using that type of function. Rather, I think you need to put something in your for loop that goes through the letters in text (reverse direction) and stores them in an empty string. Making an empty string to store reversed text in helps a lot.

I really like how you changed this project to have it output actual text instead of numbers. Good luck and I hope this helps!

1 Like

Not sure why people had trouble here on this one. If anything it been the vector one. Quick google search would of gave you the answer. I did this previously in another language on another site.

#include <iostream>

// Define is_palindrome() here:
bool is_palindrome(std::string text) {
    int l = 0; 
    int h = text.length() - 1; 
  
    while (h > l) 
        if (text[l++] != text[h--]) 
            return false; 
    return true;
}

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

Because vectors are zero indexed. If there are four elements in the vector, that last element will be element number 3. Visually: [0 1 2 3] rather than [1 2 3 4].

1 Like

This problem is unsolvable with the information provided… There’s been no explanation or indication of how to read, manipulate, or interact with strings as anything but indivisible units up to this point.
If the intention is to get the user to look outside the lesson for information, that’s an unclear and roundabout way of communicating. There should instead be some sort of direction that the knowledge needed for this lesson is out in the world and you have to track it down to solve the problem.
At the very least, it should be acknowledged that strings function as a vector of characters and will follow similar logic. Refusing to acknowledge the lack of preparation makes it look like a mistake rather than part of the teaching method.

6 Likes

If the intention is to get the user to look outside the lesson for information, that’s an unclear and roundabout way of communicating. There should instead be some sort of direction that the knowledge needed for this lesson is out in the world and you have to track it down to solve the problem.

You should get used to looking everything up in Google, StackOverflow, and other sites that house information on the language itself. That will never change and be the norm anywhere you go or do in programming. A web search would of gave you the answer to this like many things in programming.

It sounds like you think that the lesson is that you should look through online resources to figure out problems you don’t have the tools to solve, that’s how you’re going to learn programming. I agree.

My whole point is that this concept is communicated poorly by the lesson as it’s laid out. Instead of listing useful resources like StackOverflow or directing the learner to search engines the way a teacher would refer a student to the library, the lesson asks you to do something arbitrary without acknowledging you have not been given the tools necessary to interface with the problem. That’s a poor teaching method and leads to needless confusion and frustration.

Even a verbatim copy of your reply would be a significant improvement over the flat wall presented in the lesson as you provided useful recommendations and direction for a student to orient themselves towards a solution.

1 Like

Agree completely with arc.

While I certainly see where psmilliorn is coming from–and his point about searching is most valid–in terms of lesson quality, I have to heartily disagree with this whole idea of “duckduckgo is your friend” as a teaching methodology. As a resource, yes, of course. I’ve done a fair amount of professional translation over the years, and even I need to occasionally consult with other professionals online or look up certain phrases in the wild, in online dictionaries, etc.

Coming back to this lesson, here’s where web searching can fail hard. I did go to stackexchange and a couple other places, seeking a method to reverse the characters in a string. What I ended up finding was a lot of more powerful and sophisticated methods of character swapping (mostly relying on additional libraries)… and I ended up wasting an hour of time and retaining almost nothing. When I finally viewed the suggested solution, I thought, “Why the heck didn’t they just introduce a 15-minute lesson to talk about strings and demonstrate how they can be handled in a fashion similar to vectors?” This would have been far more effective and reinforced that general concept of handling data sets.

I agree that searching online can be a useful tool, but for beginners, there are potentially serious drawbacks. For one thing, a beginner cannot gauge the quality or timeliness of an answer/resource or sense very well how informed the poster of that information is. Another thing is that (again, for a beginner) this process can often take one very far afield of what they’re trying to learn. For the teacher, it’s, ahem… a cop-out.

2 Likes

// Define is_palindrome() here:
bool is_palindrome(std::string text){
int l=text.size()-1;
std::string x;
for(int i=0;i<l;i++){
x[i]=text[l-i];
}
for(int i=0;i<l;i++){
if (x[i]==text[i]){
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”;

}

maybe this will help!!!

Hello,

I have also one question. n. Please see the code bellow:

bool is_palindrome(std::string text) {
std::string reversed_text = “”;

for (int i = text.size()-1; i >= 0; i–) {
reversed_text += text[i];
std::cout<< reversed_text<< “\n”;
}
std::cout<< reversed_text<< “\n”;
if (reversed_text == text) {
return true;
}

return false;

}

int main() {

//std::cout << is_palindrome(“12345”) << “\n”;
// std::cout << is_palindrome(“ad”) << “\n”;
std::cout << is_palindrome(“121”) << “\n”;

}

This will output:
1
12
121
1.
If I take out the “-1” from for (int i = text.size()-1; i >= 0; i–) I will have the code:

bool is_palindrome(std::string text) {

std::string reversed_text = “”;

for (int i = text.size(); i >= 0; i–) {
reversed_text += text[i];
std::cout<< reversed_text<< “\n”;
}
if (reversed_text == text) {
return true;
}

return false;

}

int main() {

//std::cout << is_palindrome(“12345”) << “\n”;
// std::cout << is_palindrome(“ad”) << “\n”;
std::cout << is_palindrome(“121”) << “\n”;

}

the output will be:
1
12
121
0
Why now is not true any more?
Could you please help me understand this?

1 Like

I came here with the exact same question, would appreciate a reply if you figured it out.

I agree. The whole series of interactive lessons in functions have been like this. I would have expected to have been shown how to do some of this stuff before applying the learning. If I have to web search how to do everything, all I’m learning to do is search for answers and not understand the code, why it’s there and what it means.

1 Like

I’m having trouble with it because there haven’t been preparatory lessons and materials on getting to the solution. I’m not being slow on the uptake - you can only know what you know!

P.s. I am aware you can web search the answer!

Figured I’d give a little help for this one since I don’t think it’s super clear what to do if you’re a beginner.

First, you need to loop backwards over the passed in string so that you can build a reversed string from it. In order to do this, you need to know the final index of the original string. This is the size of the string minus one. So for the word “madam”, the size is 5 since it’s five characters. However, strings like vectors and arrays start counting at zero, so text[0] is “m”, text[1] “a”, text[2] “d”, text[3] “a”, text[4] “m”. So, the final index (the index of the last letter) is 4, the size minus 1.

Once you have that sorted the rest isn’t too hard, loop through and append each character to the new string, then return a comparison of the two strings:

#include <iostream>

// Define is_palindrome() here:
bool is_palindrome(std::string text) {
  std::string backwards_string;
  int final_text_index = text.size() - 1;

  for (int i = final_text_index; i >= 0; i--) {
    backwards_string += text[i];
  }
  
  return backwards_string == text;
}

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