FAQ: Code Challenge: C++ Functions - Palindrome

Here’s my answer. I used two functions because for some reason the .lenght() function wouldn’t work when called in a bool function.

std::string reverse_letters(std::string input) {
std::string output = "";
for ( int i = input.length(); i != 0; i--) {
  output += input.at(i-1);
}
return output; 
}

bool is_palindrome(std::string text) {
if (text == reverse_letters(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";
  
}

This is probably a stupid question, and I apologize beforehand, but it seems like it’s using text as a vector (iterating backwards through the index), yet it’s never initialized as a vector, nor any vector defined, text is only defined as a string. I don’t understand, and I am the type of person who needs to understand how something works in order to commit it to memory.

1 Like

Can someone explain this?

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

I’ve seen this before, it’s like going down, but what does it do? How does this check if its a palindrome? ELI5, thanks.

It doesn’t check if its palindrome. It’s what is referred as a reverse loop. It iterates text backwards. It takes reversed_text variable and appends text[i] to it.

i think we’re supposed to think of strings as vectors but with characters as elements

So I’ve tried this on my first attempt

#include <iostream>
#include <string.h>
// Define is_palindrome() here:
bool is_palindrome (std::string text)
{
  int x=text.length();
  int z=x/2;
  std::string check=text;
  std::string pre="";
  if (x%2!=0)
  {
    for (int i=0; i<z; i++)
    {
      pre=text[i];
      text[i]=text[x--];
      text[x--]=pre;
    }
  }
  else
  {
    for (int i=0; i<=z; i++)
    {
      pre=text[i];
      text[i]=text[x--];
      text[x--]=pre;
    }
  }
  if (check==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";

}

which I’ll admit isn’t the most optimal solution but I don’t get why it gives me an error saying it cannot convert string to char for text[x--]=pre;. Can anyone give me clear and simple answer as to why this error appears?

Its like saying we want to take this sentence and turn it into a single character.

But both of them are initialized as strings and not chars.

Everyone is really making this challenge way harder than it is. I mentioned in this thread that a quick google search would reveal not only how to do this, but is best practice to get used to doing when you need help.

#include <stdio.h> 
#include <string.h> 
  
// A function to check if a string str is palindrome 
void isPalindrome(char str[]) 
{ 
    // Start from leftmost and rightmost corners of str 
    int l = 0; 
    int h = strlen(str) - 1; 
  
    // Keep comparing characters while they are same 
    while (h > l) 
    { 
        if (str[l++] != str[h--]) 
        { 
            printf("%s is Not Palindrome", str); 
            return; 
        } 
    } 
    printf("%s is palindrome", str); 
} 

https://www.geeksforgeeks.org/c-program-check-given-string-palindrome/

Now, I get this isn’t the exact answer, but in five minutes you could convert this C script into C++ and see how easy this is and why the lesson did not have a lot of detail to begin with. Your going to have trouble converting this unless you do your work in an IDE like Visual Studio Code. Once you get used to doing your work in there it will not only help you but it will also tell you the errors as you go.

DUDE SAME I solved this completely differently. I don’t understand the way they did it ? I’m breaking my head trying to figure out what they did. I don’t understand how using Int type and textsize() helped them determine wether it was a palindrome or not, and I don’t understand what += is.

Anyways Heres what I did:

#include

// Define is_palindrome() here:
bool is_palindrome(std::string text) {
if (text == “madam”) {
return true;
}
else if (text == “ada”) {
return true;
}
else if (text == “lovelace”) {
return false;
}
}

int main() {

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

}

I have programmed in C for a long time, and this exercise also stumped me a little bit. While trying it, I was thinking “this would be easy to do in C, but I am not sure how to do it in C++”. In C, there is no elementary type of “string”, a string is an array of characters, or a pointer to characters, and can be manipulated as such. But based on the lessons so far, the string is not introduced in this way. In fact a string is introduced as an “elementary type”, just like an int or double. If I wanted to manipulate the third digit of an int variable x, I would not try to reference x[2], so why should I think that, based on the info provided so far, I should be able to manipulate the third character of a string s by trying to reference s[2]? It would have been nice if the lessons had made it clearer what the string type actually is. Furthermore I would never have guessed that the += operator would have performed an append operation to a string type, trying to do that operation on a string in C will likely destroy the string.

I guess I would ask that the mystery behind the string type be cleared up a little bit in the explanatory lessons on this type.

If you Google

+= in c++

You would get this as the first result
http://www.cplusplus.com/doc/tutorial/operators/
On that page if you search you would get

Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)
Which is

y += x;	y = y + x;

Hello, @pipo_04, and welcome to the forums!

Did that code actually pass? That’s not a solution. Arbitrarily returning true or false because you, the programmer, can see whether or not a word is a palindrome is not the same as writing a function that instructs the computer how to determine whether or not a word is a palindrome.

There are many ways this task can be accomplished. The hint for the exercise suggests iterating over each word (one letter at a time), and saving the letters to a new variable in reverse order. Then compare the original word with the reversed word. If they’re the same, the word is a palindrome, if not, it isn’t.

I’m including my solution here which as you’ll see took the task a step or two farther. I’ve included comments throughout to explain things. Review it, if you like, and perhaps something useful can be gleaned.

#include <iostream>
#include <array>
#include <locale>
using std::array;
using std::string;
using std::cout;
using std::locale;
using std::tolower;

//Array of test strings:
array<string, 7> words = {"madam", "ada", "lovelace", "pizza", \
  "Was it a car or a cat i saw?", "Murder for a jar of red rum.", \
  "Racecar"};

// Define is_palidrome() here:
bool is_palindrome(string text) {
  string r_text = ""; //reversed o_text
  string o_text = ""; //original text (converted)
  locale loc; //necessary to use tolower()
  
  //iterate over the original text one character at a time
  //if the character is alphabetic, convert it to lower case
  //(if necessary), and add it to o_text
  for (int i = 0; i < text.size(); i++) {
    if(isalpha(text[i])) {
      o_text += tolower(text[i], loc);
    }
  }
  
  //iterate over o_text from the right adding each letter
  //to r_text starting from the left (reverses the text)
  for (int i = o_text.size() - 1; i >= 0; i--) {
    r_text += o_text[i];      
  }
  
  //print o_text and r_text to visually confirm results
  cout << o_text << "\n" << r_text << "\n";
  
  //returns 1 (true) if they are equal; 0 (false) otherwise
  return r_text == o_text; 
  
}

int main() {
  //iterate over the words array passing each value to the
  //is_palindrome() function & print the results
  for (int i = 0; i < words.size(); i++) {
    //use a ternary expression to assign " is " if the return value
    //is true, or " is not " if the value is false to result
    string result = is_palindrome(words[i]) ? " is " : " is not ";
    cout << "\"" << words[i] << "\"" << result << "a palindrome.\n\n";
  }
}

Output:

madam
madam
“madam” is a palindrome.

ada
ada
“ada” is a palindrome.

lovelace
ecalevol
“lovelace” is not a palindrome.

pizza
azzip
“pizza” is not a palindrome.

wasitacaroracatisaw
wasitacaroracatisaw
“Was it a car or a cat i saw?” is a palindrome.

murderforajarofredrum
murderforajarofredrum
“Murder for a jar of red rum.” is a palindrome.

racecar
racecar
“Racecar” is a palindrome.

I didn’t understand how everyone else “reverses the character,” so this is my take.

  std::string word = "lovelace";
  std::string rword = word;
  
  for (int i = word.size()-2; i >= 0; i--) {
    rword.push_back(rword[i]);
   
  } 
    rword.erase(0, word.size()-1);

   std::cout << word;
   std::cout <<rword;