-1

I've got a recursive function that is supposed to check if a given word is a palindrome, which works by comparing the first and last value, then strips them off and passes them recursively into checkPalindrome, but whenever I try to assign word[size-1] to '\0', I get a Bus Error.

Here is my code:

bool checkPalindrome(char word[]){
  int size = std::strlen(word);
  //Parts removed...
  word[size-1]='\0'; //This is the line causing the issue
  return checkPalindrome(word+1);
  }
}

Here is an example of a call to this function:

checkPalindrome("racecar");

Thank you!

Weston Reed
  • 187
  • 2
  • 9

2 Answers2

2

There are two ways I can think of to solve this problem:

The obvious one would be to use a std::string instead of a string literal and just .pop_back() (C++11) to remove the last character.

The second would be to pass the length of the string to the function as a parameter, instead of computing it. Then, just decrease the length by 1 so now you have a new "fake end point" to the string. Since you're using this number to check the last character, you don't really need to actually modify the string, just change where the "last" character is.

But since you're using C++, I don't see why not use std::string and avoid overcomplicating the problem.

Cyber
  • 857
  • 5
  • 10
  • Unfortunately I'm required to use a CString, and the function signature has to be bool isPalindromeR(char* word) – Weston Reed Oct 16 '18 at 16:48
  • @WestonReed ok then, another way would be to create a new string altogether. It's a bit nasty but you could `std::calloc` a new `char*` of size `size` (since you also want space for the null character at the end), then `std::memcpy` `size-1` bytes of `word` into the new string, and pass the new string to the function recursively. `std::calloc` guarantees that the last character is set to `0` so the string is valid. This can go wrong in many ways, so always remember to call `std::free` on the newly allocated string after the recursive call so you don't leak memory. – Cyber Oct 16 '18 at 16:55
  • @WestonReed careful with the term "CString". There is a class that used to be in wide use in Microsoft-space [called `CSTring`](https://learn.microsoft.com/en-us/cpp/atl-mfc-shared/using-cstring?view=vs-2017), and this may cause confusion among folks – user4581301 Oct 16 '18 at 16:55
  • Given that restriction, you could do something like `char testStr[] = "racecar"; std::cout << std::boolalpha << isPalindromeR(testStr) << '\n';` – Daniel Schepler Oct 16 '18 at 16:56
  • Unfortunately, test cases are being passed as string literals, so that solution won't work. – Weston Reed Oct 16 '18 at 16:57
  • Not sure I'd fall all the way back to C with `calloc` and `free`. `new char[size+1]` and null-terminating manually is closer to idiomatic C++. – user4581301 Oct 16 '18 at 17:00
  • Under these requirements you probably have to create a new smaller string before each recursive call. – drescherjm Oct 16 '18 at 17:00
  • @WestonReedok yet another way would be to create a `std::string` from your string literal, use `.pop_back()` or `.erase()` to delete the last character, and then convert it back to a string literal with `.c_str()`. – Cyber Oct 16 '18 at 17:02
  • @CyberNiki "*and then convert it back to a string literal with `.c_str()`*" - that is not what `c_str()` does. It returns a pointer to a null-terminated C string. That is not the same thing as a string literal – Remy Lebeau Oct 16 '18 at 23:18
1

So as stated in the comments you can't modify a string literal. So before passing it to the function, make a copy of it.

std::size_t len = std::strlen(word);
char* copy = new char[len + 1];
std::strncpy ( copy, word, len + 1 );
isPalindromeR(copy);
delete[] copy;

other solution would be to not use recursion, thus not needing to modify the string.

anyway outside of assignments use a std::string

Bob Bills
  • 519
  • 4
  • 8
  • I would upvote this answer if it was a complete answer modifying isPalindromeR(). I believe the requirements are you have to allow the string literal passed to isPalindromeR(). Although it's not clear if you can just create a second function that does the recursion. – drescherjm Oct 16 '18 at 17:08