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";
  
}
1 Like

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.

1 Like

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.

1 Like

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;

Oops late reply…

https://pastebin.com/Bbers5Kp

That is my solution.

I wrote 2 extra supporting functions along the top.

bool isEven(std::vector myVector)

bool isVectorEqual(std::vector firstVector, std::vector secondVector)

Anyways the pseudo-code for my design is this.

First I convert the String into a vecor char Array. (Line 37 to 43)

I first check if the char entered in the function is EVEN (abab) or ODD (abcba).

Example: Even char vector “abab” array…
Odd char vector “abcba” array…

If it is EVEN, I take the first 2 char and compare it to last 2 char using a for loop.
One scans from ab, and the other scans from ba. (Line 54 to 83)

ab != ba, thus the function is_palindrome(“abab”); should return FALSE.

If it is ODD, I ignore the middle char, compare the first 2 char ab and the last 2 char ba using a for loop. One scans from ab, and the other scans from ab. (Line 84 to 112)

ab == ab (Equal), thus the function is_palindrome(“abcba”); should return TRUE.

If you do it yourself, in your decrement loop, make sure it’s something like this.
for (int k=charVector.size()-1; k> (charVector.size()-1)/2; k–)

If you forgot to decrease 1 from the initial k, the decrement loop will go OUT OF BOUNDS!!
This code is a source of headache. “k> (charVector.size()-1)”

I literally traced the code using paper. https://photos.app.goo.gl/gwkNchG696hSL2qa8

Lets say you have a VECTOR with size 7. It is stored in computer labeled 0,1,2,3,4,5,6…
You have to go through position 0, 1, 2… ignore 3… position 4, 5, 6.

Remember, the SIZE function will return 7.

If you count from 0, it is easy.

for (int i=0; i< (charVector.size() - 1) /2; i++) //middle function will return 3
{
//Scan position 0, 1, 2, which is correct.
}

But if you count from end to middle, you have to do this.

for (int k=charVector.size()-1; k> (charVector.size()-1)/2; k–) //initial=6, middle=3, decrement
{
//Scan from position 6, 5, 4… the code ignores position 3 because the conditional is “greater than”.
}

Imagine trying to find the bug when your initial condition at a loop is out of bounds!!
I wanted to rip my hair out at this project, but I did it!! YAAAAAAAAAAY!!

Cause every element in this group is indexed from 0.

for example
std::string txt=“abc”;

here “a” is indexed as 0
"b"is indexed as 1
“c” is indexed as 2

Another possible method is to use the inbuilt reverse function in the “algorithm” header file.

#include <iostream>
#include <algorithm>

// Define is_palindrome() here:
bool is_palindrome(std::string text) {
    
    std::string rev = text;
    std::reverse(rev.begin(), rev.end());

    if (rev == 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";

}

I agree. Even though we can learn it fine, it’s harder to remember and periodic practices could help with that.

1 Like

I did it using vectors. However, I see there are easier solutions for it.

#include <iostream>
#include <stdint.h>
#include <cstdlib>
#include <vector>
#include <string>

using namespace std;

// Define is_palindrome() here:
bool is_palindrome(std::string text){
  bool res=false;
  vector<char> vec(text.begin(), text.end());
  vector<char> vecr;
  for (int i=(vec.size()-1); i>=0; i--){
    vecr.push_back(vec[i]);
  }

  for (int i=vec.size(); i>=0; i--){
    if (vec[i]==vecr[i]){
      res=true;
    }
    else{
      res=false;
    }
  }

  /*cout<<"vec.size= "<<vec.size()<<"\n";
  cout<<"vecr.size= "<<vecr.size()<<"\n";

  for (int i=0; i<vec.size();i++){
    cout<<"vec["<<i<<"]="<<vec[i]<<"\n";
  }

  for (int i=0; i<vecr.size();i++){
    cout<<"vecr["<<i<<"]="<<vecr[i]<<"\n";
  }  
//or
  for (int i : vec){
    cout<<(char) i<<"\n";
  }
  for (int i : vecr){
    cout<<(char) i<<"\n";
  }  
    */
  return res;
}

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

pipo_o4 Please kindly check what Palindrome is !

Happy Coding <3