What is wrong with my palindrome function? C++

Hi, I’ve been spending some time now trying to figure out what’s wrong with my comparison but I just can’t seem to find why it’s not working…

Here’s my code with some debug:

#include <iostream>

// Define is_palindrome() here:
bool is_palindrome(std::string text) {
  
  std::string compare = "";
  std::string original = "";
  int length = text.length();
  
  
  // reverse string
  for (int i = length; i >= 0 ; i--) {
    compare += text[i];
    std::cout << "Compare variable contains: \"" << compare << "\"\n";
  }
  
  // something is wrong, testing to build my own string variable 
  // the same was (but front to back) as the compare string 
  // the input text string
  for (int i = 0; i < length ; i++) {
    original += text[i];
    std::cout << "Original text variable contains: \"" << original << "\"\n";
  }

  // Looks the same to me
  std::cout << "Compare = \"" << compare << "\"\n";
  std::cout << "Original = \"" << original << "\"\n";
  
  
  // it does not match between compare and original
  if (compare == original) {
    std::cout << "It matches\n";
    return true;
  } else {
    std::cout << "It does NOT match\n";
    return false;
  }
  
  
}

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

Here’s my output:

Compare variable contains: ""
Compare variable contains: "m"
Compare variable contains: "ma"
Compare variable contains: "mad"
Compare variable contains: "mada"
Compare variable contains: "madam"
Original text variable contains: "m"
Original text variable contains: "ma"
Original text variable contains: "mad"
Original text variable contains: "mada"
Original text variable contains: "madam"
Compare = "madam"
Original = "madam"
It does NOT match
0
Compare variable contains: ""
Compare variable contains: "a"
Compare variable contains: "ad"
Compare variable contains: "ada"
Original text variable contains: "a"
Original text variable contains: "ad"
Original text variable contains: "ada"
Compare = "ada"
Original = "ada"
It does NOT match
0
Compare variable contains: ""
Compare variable contains: "e"
Compare variable contains: "ec"
Compare variable contains: "eca"
Compare variable contains: "ecal"
Compare variable contains: "ecale"
Compare variable contains: "ecalev"
Compare variable contains: "ecalevo"
Compare variable contains: "ecalevol"
Original text variable contains: "l"
Original text variable contains: "lo"
Original text variable contains: "lov"
Original text variable contains: "love"
Original text variable contains: "lovel"
Original text variable contains: "lovela"
Original text variable contains: "lovelac"
Original text variable contains: "lovelace"
Compare = "ecalevol"
Original = "lovelace"
It does NOT match
0

Please advice, thanks!

1 Like

This part of the output looks quite suspicious:

Compare variable contains: ""

Why this happens? Figure this out, make small modification and your code will yield correct results.


Hint - if string has lenght 8, index of the last character is 7.

1 Like

Thanks for the hint! I was staring blind at this apperently.

I modified the loop, but it still did not pass validation until i removed the std::cout for it to verify.

Could you perhaps explain why the strings looked the same when printing them but did not match?
Thanks once again, now I can get moving :slight_smile:

1 Like

If they don’t compare as equal then they’re probably not the same.
So you’d want to inspect their data as opposed to … eyeballing whatever shows up on screen.
Printing out the bytes is one option. Using a debugger is another.

(If you haven’t already) you’re going to want to figure out where the extra character came from to better understand how to fix it next time it happens, or perhaps even learn to avoid it (but realistically you are going to introduce something like this quite often when dealing with cpp, especially as a beginner. As you get more experienced you learn to eliminate scenarios where these mistakes can exist by abstracting away the dirty details…There’ll always be bugs though)

### compile with debug flag (-g)
$ g++ -Wfatal-errors -std=c++17 -Wall -g a.cpp

### start gnu debugger with the executable
$ gdb a.out
Reading symbols from a.out...

### add breakpoint at "does not match" print (line 35)
(gdb) break 35
Breakpoint 1 at 0x250c: file a.cpp, line 35.

### run until breakpoint
(gdb) run
Starting program: /tmp/derp/aoetuh/a.out
Compare variable contains: ""
Compare variable contains: "m"
Compare variable contains: "ma"
Compare variable contains: "mad"
Compare variable contains: "mada"
Compare variable contains: "madam"
Original text variable contains: "m"
Original text variable contains: "ma"
Original text variable contains: "mad"
Original text variable contains: "mada"
Original text variable contains: "madam"
Compare = "madam"
Original = "madam"

### the breakpoint was reached
Breakpoint 1, is_palindrome (text="madam") at a.cpp:35
35          std::cout << "It does NOT match\n";


### look at variables
(gdb) info locals
compare = "\000madam"
original = "madam"
length = 5
5 Likes

in the //reverse string to access the index from the back its supposed to be length-1 i.e.
// reverse string
for (int i = length - 1; i >= 0 ; i–) {
compare += text[i];
std::cout << “Compare variable contains: “” << compare << “”\n”;
}
indexes start at 0 so if the length is 5 the index positions will be 0, 1, 2, 3, 4.
i hope this explains it

1 Like

in the //reverse string to access the index from the back its supposed to be length-1 i.e.
// reverse string
for (int i = length - 1; i >= 0 ; i–) {
compare += text[i];
std::cout << “Compare variable contains: “” << compare << “”\n”;
}
indexes start at 0 so if the length is 5 the index positions will be 0, 1, 2, 3, 4.
i hope this explains it

1 Like

Thank you it all makes sense now :grin::smile: