char string doesn't work what the problem with "\0"

char pointer c++
char array
string in c
c++ string
char pointer array in c
char array c++
string to char array c++

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;
       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);
return 0;}

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

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 cant 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.

Also, please post your exact error message in the future. As a matter of fact, please edit your answer and add that now, to help answerers and future readers.

Note: Answered from my phone so I can't test or be as thorough as I'd like. Maybe I can come back to this later to improve my answer.

Why is char *A able to hold strings while char A cannot?, Char pointers are assumed to point to the beginning of a string. The pointer itself points to the first character in the string, and code using the� String literals (such as "Hello") have type char[N] where N is number of characters (including the terminating '\0'). An array can be converted to a pointer to its first element, but arrays and pointers are not the same thing, whatever some bad books or teachers may say.

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

C - Strings, C - Strings - Strings are actually one-dimensional array of characters terminated by a null character '\0'. Thus a null-terminated string contains the characters that� A string pointer declaration such as char *string = "language" is a constant and cannot be modified. Summary. A string is a sequence of characters stored in a character array. A string is a text enclosed in double quotation marks. A character such as 'd' is not a string and it is indicated by single quotation marks.

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'.

C Strings (Arrays vs. Pointers), However, pointers only hold an address, they cannot hold all the characters in a character array. This means that when we use a char * to keep track of a string,� void Send_AT_CMD( char const str[], int n_chars ); void Send_AT_CMD( char const * str, int n_chars ); This has a variety of benefits including letting you pass string literals to the function without getting compilation errors/warnings, and letting users and the compiler know that strings passed to the function won't be modified.

C strings and C++ strings, Here are some examples of declaring C strings as arrays of char : Comparing C strings using the relational operators == , != , > , < , >= , and <= does not work� The includes() method determines whether a string contains the characters of a specified string. This method returns true if the string contains the characters, and false if not. Note: The includes() method is case sensitive.

C Programming Course Notes - Character Strings, The C language does not have a specific "String" data type, the way some other languages such as C++ and Java do. Instead C stores strings of characters as� A string is made up of many characters. A character is a primitive, likewise, it doesn't "contain" any other items. A string is basically an array of characters. For comparing string and characters: char a = 'A'; String alan = "Alan"; Debug.Assert(alan[0] == a); Or if you have a single digit string.. I suppose

String Copy - How to play with strings in C, The destination character array doesn't have to be initialized. It can be left uninitialized and can be passed to strcpy . Still, it must have enough space to hold the� Size of string literals. For ANSI char* strings and other single-byte encodings (but not UTF-8), the size (in bytes) of a string literal is the number of characters plus 1 for the terminating null character. For all other string types, the size isn't strictly related to the number of characters.

  • 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?
  • Does this answer your question? Single quotes vs. double quotes in C or C++
  • Explain your problem in more detail. "My code doesn't work" and "the erroe is '\0'" are not good explanations of the problem.
  • 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
  • @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.
  • 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 and Gabriel, thank you. How can I initialize the rest of n? I thought, I initialized it
  • 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?
  • Again, "it doesn't work" is not a valid problem description. Please put more time into a scientific explanation and exploration of your issues.
  • @JanylS If you've fixed the compile-time error, then you've moved on to a new question.
  • Where does this quote come from?
  • thank you very much, and the next question is, that my main function is wrong. Do you know maybe where is the error? I get every time 0 as output(
  • This was already stated, half an hour ago. What about the other issues in the code?