 # Don't understand palindrome function

Basically, I was finishing the functions projects, and it asked me to make a palindrome test to check whether an input was a palindrome or not.

`#include

// 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 += text[i];
}

if (reversed_text == text) {
return true;
}

return false;

}

int main() {
std::cout << is_palindrome(“madam”) << “\n”;
std::cout << is_palindrome(“ada”) << “\n”;
std::cout << is_palindrome(“lovelace”) << “\n”;
}`
That’s the code there, could someone please break this down step-by-step? I need help understanding how it itterates over the text to reverse it. Thanks.

ps sorry for any formatting mistakes, this is my first post.

1 Like

Typically in a `for` loop you would start at 0, and iterate forwards or upwards to the end of an array or in this case a string. Because `i` is initially assigned a value equal to the length or size of the string (number of characters in the string) like so:`int i = text.size() - 1` we are starting from the last character, and iterating backwards or downwards to the beginning of the string.
Let’s take “ada” for example. A string is essentially an array of characters, so the character at index 0 is ‘a’, at index 1 is ‘d’ and at index 2 is ‘a’. (indexes start at 0)
The .size() of “ada” is 3, but there is no character at index 3, so we initialize the index variable to `.size() - 1` to account for that: `for (int i = text.size() - 1; i >= 0; i--)`. Now while `i >= 0` we add the character at index `i` to the `reversed_text variable`, and then subtract 1 from `i`.
Hope this helps!

13 Likes

What is the meaning of ‘+=’ symbol in the above question?

With regards to string values (text) `+=` is a concatenation operator. It adds or ‘appends’ the value on the right to the existing value on the left. It is basically shorthand for: `reversed_text = reversed_text + text[i]`

2 Likes

Hello, @nicolasa2776731346!

Let’s look at an example:

``````#include <iostream>

int main() {
std::string text = "cat";
std::string reversed_text = "";

//text.size() is 3 since 'cat' has 3 characters
//the indexes of those 3 characters are accessible
//using [] like this: text
//the character at the index 0 is 'c'
//so text refers to 'c', text refers to 'a'
//and text refert to 't'

for (int i = text.size() - 1; i >= 0; i--) {
//we start out with i equal to 2 since: (3 - 1 = 2)
//we want to start at 2 because 2 is the index for the last character in text
//so text is 't'

reversed_text += text[i];
//reversed_text was empty, but now has a value of 't'
//because we added 't' to the end of its previous value
//of ""
}
//the code inside of the { } above repeats as long as i
//is greater than or equal to 0 because of the second
//expression in our for loop: (i >= 0;)
//this will cause text which is 'a'
//and text which is 'c' to be added
//in turn to the end of reversed_text the
//same way that text which was 't' was added
//once i is less than 0, the loop stops executing, and
//the lines of code below are executed

std::cout << "Original value of text: " << text << "\n";
std::cout << "Value of reversed_text: " << reversed_text;
}
``````

Output:

Original value of text: cat
Value of reversed_text: tac

Hopefully this helps!

6 Likes

Here’s the way I coded this challenge, maybe it’ll make sense to you. Instead of creating a reverse string this code tests the two ends of the original text to see if they match. If they do, it assigns true to the bool val and then moves in one spot on both sides of the original text. It will do this until it hits or passes the middle of the word, which is why in the for loop the second parameter is i < text.size()/2. If at any time the function finds two letters that don’t match, it returns false ending the function. At the end, if all matches were true the function will return val which is true. Let me know if more explanation is needed.

#include

// Define is_palindrome() here:
bool is_palindrome(std::string text) {
bool val;

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

return val;
}

int main() {

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

}

Hello, @blog2881331944! Welcome to the forum.

That is another way to do it. You could refactor your code to make it even more efficient like so:
SPOILER:

``````#include <iostream>

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

for(int i = 0; i < text.size()/2; i++) {
if(text[i] != text[text.size()-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";
}
``````

Happy Coding!

1 Like

Awesome! I see the logic here, this is a technique I can apply to a lot of code to simplify it. Thanks.

2 Likes

Why is it not possible to just call the is_ palindrome() function? why do we have to use the print out statements?

Printing the result is simply visual confirmation that the function performed as expected.