-1

I don't know what the problem is? My code doesn't work. :(

#include <iostream>
#include <string.h>

using namespace std;

bool isPalindrome(char* word){
   int len = strlen(word);
   if(len <= 1){
       return true;
   }else
       if(word[0] == word[len-1]){
           char n[len-1];
           n[len-1]= "\0";
           return isPalindrome(n);



       }
   return false;

}

int main(){

char *a = "alla";
bool b = isPalindrome(a);
cout<<b<<endl;
return 0;}

The error is "\0" , I don't know why. My main function should be not right too.

Janyl S
  • 49
  • 6
  • 2
    That is not valid C++. `char n[len-1]` is incorrect. `n[len-1] = "\0";` has two bugs, it is writing past the end and assigning a string rather than character. Did you even try compiling this first? – Tanveer Badar Nov 23 '19 at 16:41
  • Does this answer your question? [Single quotes vs. double quotes in C or C++](https://stackoverflow.com/questions/3683602/single-quotes-vs-double-quotes-in-c-or-c) – JaMiT Nov 23 '19 at 17:02
  • Explain your problem in more detail. "My code doesn't work" and "the erroe is '\0'" are not good explanations of the problem. – Lightness Races in Orbit Nov 23 '19 at 17:11
  • The same problem is me too. with char n[len-1] = "\0" and char n[len-1] I would like to remove the first and last characters from the string – Janyl S Nov 23 '19 at 17:17
  • @JanylS Your edit just invalidated the answers you've already received. This is unfair to the people who answered and confusing for the next person to come with the same question as you. Please revert the edit and start a new question for your new problem. – JaMiT Nov 23 '19 at 17:57
  • yes, sorry, I reverted it. I'm new in stackoverflow and in programming – Janyl S Nov 23 '19 at 18:06

3 Answers3

1

I believe "\0" is a null-terminated 2 character C string consisting of a zero followed by a zero. Use '\0' instead, which is just a zero. Remember: use double quotes for strings and single quotes for single characters. You cannot assign a string to a string index location, so double quotes don't work like that, but you can assign a character to a string index location.

2nd error: read the documentation on strlen(). Create n with char n[len + 1];, NOT char n[len-1];, in order to make n the same length as the other string. Then, null terminate with n[len]= '\0';, not n[len - 1]= '\0';, since strlen doesn't count the null terminator in the string. I'm confused by your code though: what is the purpose of this n string? Lastly, you're writing outside your n array as you have it written! Since you made n have size len - 1, you would need to null terminate at index len - 2. len - 1 in your case is outside the array! Always null terminate inside the array at the index 1 smaller than the size of the array. When the compiler knows the side of the array, such as is the case with n, do it like this instead: n[sizeof(n) - 1] = '\0';.

You can't use a variable to set an array length in C by the way, and in C++ it may require len to be const to instantiate an array.

halfer
  • 19,824
  • 17
  • 99
  • 186
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • 1
    I believe assignment `n[len-2]= '\0'` is the correction, given a little guesswork. It looks to me like the intent was to remove the first and last characters from the string. If the result is a palindrome, then the original string is one (in that `if` clause). A bigger issue is that the rest of `n` is not initialized. (The copy from `word+1` is missing.) – JaMiT Nov 23 '19 at 17:06
  • JaMiT and Gabriel, thank you. How can I initialize the rest of n? I thought, I initialized it – Janyl S Nov 23 '19 at 17:13
  • Why do you think that? Where did you do that? Can you point us to the place in the code where you set any other value in that array? – Lightness Races in Orbit Nov 23 '19 at 17:18
  • I removed the first and the last string. My problem was that I write "\0" instead '\0'. But my next problem is that my main function doesn't work ( – Janyl S Nov 23 '19 at 17:24
  • Where did you do that? – Lightness Races in Orbit Nov 23 '19 at 17:25
  • 1
    Again, "it doesn't work" _is not a valid problem description_. Please put more time into a scientific explanation and exploration of your issues. – Lightness Races in Orbit Nov 23 '19 at 17:26
  • I would like to test if "anna" Palindrom is or not, my main function should output "true", but I get every time "0" as output – Janyl S Nov 23 '19 at 17:36
  • char n[len-1]; n[len-1]= '\0'; It should remove the last and the first string – Janyl S Nov 23 '19 at 17:48
  • 2
    @JanylS If you've fixed the compile-time error, then you've moved on to a new question. – JaMiT Nov 23 '19 at 17:55
1

The Simple answer is that you should

use '\0' instead of "\0"

as n is a character array that you should use single quotation instead of double quotation

Hope this will Help

GameChanger
  • 179
  • 11
1

The assignment of element of character array should be with character only but in your case you are assigning to string. Note that ''/0'' is string. Try using assigning to '/0'.

anuplohiya
  • 46
  • 3