FAQ: Code Challenge: C++ Functions - Palindrome

If you compare the length of text and reverse, you will see that reverse has one extra character, even though the two strings seemingly appear to be the same.

In your snippet posted above, reverse doesn’t end up being " madam". Instead of a blank space, a null character (denoted by '\0') has ended up being the first character of your reverse string. So, reverse is actually "\0madam" which appears as "madam" on your screen. On screen, the text and reverse strings will appear indistinguishable, but if you check their length, you will see that the length of text is 5 while that of reverse is 6.
(You can also confirm this by a statement such as std::cout << (reverse[0] == '\0') << "\n" which will output 1 if true and 0 if false)

If you edit the loop initialization to int i = text.size() + 1; or some other out of range index number, you will see undefined behavior (your output could be gibberish or some other unpredictable behavior). Many languages (like Python) will throw an error. But in C/C++, the onus is on the programmer to respect the range/bounds.

My code is pretty much the exact same but the output is still just zeros.
Please help, I’m not sure why this isn’t working.

#include <iostream> #include <string> // Define is_palindrome() here: bool is_palindrome(std::string text) { std::string reverse; for (int i = text.length(); i >= 0; i--) { reverse.push_back(text[i]); } return (text == reverse); } int main() { std::cout << is_palindrome("madam") << "\n"; std::cout << is_palindrome("ada") << "\n"; std::cout << is_palindrome("lovelace") << "\n"; }

Have a look at the loop variable initialization in your for loop.
From earlier in the thread (same logic applies to .size() and .length() ):

1 Like

Same here, but I think the experience of making programs frequently will help us understand this exercise, I will finish it with the solution they provide, then I will try to make it on my own IDE.

Why int i = text.size() - 1 instead of int i = text.size()
Thank you

The index of the last character in text would be text.size() - 1

This is what I have come up with. my logic is:
if first letter of the string equals last letter,
and, 2nd letter equals 2nd to last letter, so on…
then the string is Palindrome. Otherwise it is not.
surprisingly, it accepted it.

#include <iostream> // Define is_palindrome() here: bool is_palindrome(std::string text){ int str_size = text.size() - 1; for(int i = 0 ; i <= str_size ; i++){ for (int g = str_size; g >= 0; g--){ if( text[i] == text[g]){ 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"; }

You may want to run further tests on your function. For example,

std::cout << is_palindrome("mabcxyzm") << "\n";

Your code identifies it as a palindrome, even though it isn’t.

Your logic is sound, but the way you have implemented the logic is not quite correct. Your existing code looks at the first and last characters and makes a decision about the string being a palindrome or not. It never checks the 2nd letter against the 2nd to last character, and so on… Your code makes a decision solely on the first and last characters.

I made it like this, i wanted to put the iterates in only one, but the code made weirds values, so i separeted them. And i noticed that the text vector got empty so i copied it to text_check vector in order to check if reversed and text.check are similar. The function is a string 'cause with a bool it returns 0s or 1s. This is only using coding learned with the past lessons. Not adding any type.

#include <vector> #include <iostream> // Define is_palindrome() here: std::string is_palindrome(std::string text){ int size = text.size() ; std::vector<std::string> reversed (text.size()); std::vector<std::string> text_check (text.size()); int result = 0; for(int i = 0; i < size; i++) { text_check[i] = text[i] ; } for (int j = 0; j < size; j++ ){ reversed[j] = text[text.size() - 1 ] ; text.pop_back(); } for (int k = 0; k < size;){ if (reversed[k] == text_check[k]) { result++; k++;} else { k = size; }} if (result == size) { return "true"; } else { return "false"; }} int main() { std::cout << is_palindrome("madam") << "\n"; std::cout << is_palindrome("adddda") << "\n"; std::cout << is_palindrome("lovelace") << "\n"; }

If your goal is to actually understand what you’re doing the only way to solve the problem is to go through literal dozens of explainers on what several entire concepts are. The fact of the matter is that the course failed to inform you what scope the required functions work in, making the question the equivalent of having no guidance at all. If I wanted to learn these concepts by spending over an hour trying to figure out what is even possible do by searching through google and stack overflow, I wouldn’t be on this site at all. The question was flawed in ways that defeat the purpose of using the website, which is why people have a problem with it. If the goal was to make you spend an hour trying to figure it out with zero help at all, that should have been part of the hint as it has been on questions in which that is the goal. By defending an obviously flawed question, you are trying to keep in place something that likely made people quit trying to learn because they thought that they weren’t cut out for it when the actual problem is that they weren’t even informed of the necessary prerequisites to solving the problem.

This wasn’t so bad, the problem I ran into was that I was trying to be needlessly fancy and putting the string into a vector, then dropping letters with each iteration of the loop. Which, I think that there is a way that that would work, but it would definitely have been clunky and slow code compared to just iterating over the letters in the string.

I’m feeling this 100%. I’m finding the challenges lack context, working examples and unclear desired outcomes. I’ve come to the 100% committed but as this course progresses its becoming harder to keep focussed as the guidance is so patchy

Am I the only person that just used strrev()?

I have 2 questions, but first, here’s my solution:

#include <iostream> // Define is_palindrome() here: bool is_palindrome(std::string text){ std::string testtxt = ""; int last_lettre_index = text.size() - 1; for (int i = last_lettre_index; i >= 0; i -= 1){ testtxt += text[i]; } if (testtxt == 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"; }

This works but returns 1, 1, 0 in the terminal instead of true, true, false (this might seem stupid but I’m unsure if that’s how it’s supposed to work, it might), please let me know if this is how it’s supposed to work.
Second, could anyone please kindly explain to me why this doesn’t work?

#include <iostream> // Define is_palindrome() here: bool is_palindrome(std::string text){ std::string testtxt = ""; int last_lettre_index = text.size() - 1; for (last_lettre_index >= 0; last_lettre_index -= 1;){ testtxt += text[last_lettre_index]; } if (testtxt == 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"; }

It just returns three zeros, did I make a mistake somewhere and/or can’t I use last_lettre_index instead of declaring it as i?

Thing is though, you had to google. An important part of learning a task is the understanding of an explanation. This has put me off doing the paid course. I can look stuff up on google, chatgpt and copy paste - that isn’t learning, and isn’t something I would pay for. There are some good youtube’s, where people show you how to look stuff up.

This is the first Hiccup on C++, so not bad, and free.

“You can iterate over a string in an identical way to a vector” would have been a useful hint.

1 Like

This is my code solution for the challenge

#include

// Define is_palindrome() here:

bool is_palindrome(std::string text) {
for (int i = text.size()-1; i>=0; i–) {
for (int k = 0; i <= text.size()-1; k++ ) {
if (text[i] == text[k]) {
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”;

}

incorrect copy pasted quotation marks up the error but for headers this:
palindrom_halfsize.cpp: In function ‘bool is_palindrome(std::string)’:
palindrom_halfsize.cpp:11:72: error: expected primary-expression before ‘)’ token
11 | for (size_type i = 0, j = text_size - 1; i < text_size / 2; i++, j-);
| ^

palindrom_halfsize.cpp:14:18: error: ‘i’ was not declared in this scope
14 | if (text[i] == text[j])
| ^
palindrom_halfsize.cpp:14:29: error: ‘j’ was not declared in this scope
14 | if (text[i] == text[j])
| ^
(I try this in the BASH shell and not solved it)

In the code you have pasted, a few characters have been changed because of the copy pasting.
Specifically,

  • j– should be corrected to j--

  • The quotes such as (“madam”) << “\n” should be corrected to ("madam") << "\n"

Once these corrections are made, the code does compile and run.

Go to https://cpp.sh/ and run it

#include <iostream>
#include <string>

bool is_palindrome(std::string text)
{
    std::size_t text_size = text.size();
    for (std::size_t i = 0, j = text_size - 1; i < text_size / 2; i++, j--)
    {
        if (text[i] != text[j])
            return false;
    }

    return true;
}

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

    return 0;
}

Copy/paste from a webpage with quotations can often copy the wrong ones. I believe its different, ASCII code is different. You would basically have to delete it and add the ones from your keyboard.