-1

I've been told the null character or ‍'\0' serves a purpose to identify the end of a string.

But, I'm in a haze as I have come across two programs thus far, in which, there have been no usage of the same(null character).

All three programs deal with strlen(), and I cannot extrapolate its significance and/or presence in this one.

char st[20], rst [20]; // Edit: Added these string declarations!
int i, j;
printf("\n Enter the string : ");
scanf("%s",st);
i= 0;
j = strlen(st) - 1;
while (j >= 0)
{
    rst[i] = st[j];
    i++;
    j--;
}
rst[i] = '\0';
if(strcmp(st, rst) == 0)
    printf("\n %s is a palindrome string", st);
else
    printf("\n %s is not a palindrome string", st);

And it appears like the inclusion or exclusion of the null character does not affect the result.

But, I'm curious why the author may find it imperative to add it.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Bao
  • 25
  • 1
  • 1
  • 10
  • 1
    The code is building a new string from characters in the first one entered. Every C string must terminate with a `'\0'`. – Weather Vane Sep 29 '19 at 20:34
  • The significance of `'\0'` is to mark the end of the string. I'm not sure what you're asking. – melpomene Sep 29 '19 at 20:35
  • *"there have been no usage of the same(null character). All three programs deal with `strlen()`"* ... and there is the usage of the `'\0'` character: in `strlen()` which detects it to find the length of the string. – Weather Vane Sep 29 '19 at 20:37
  • @user3121023 no explicit mention of initialization. but, rst has been declared with an array size of 20, or rst[20]. – Bao Sep 29 '19 at 20:38
  • 2
    "writing the `'\0'` does not affect the result." That may be true, if there happened to be a `0` there already. But it is not *defined* to be there. – Weather Vane Sep 29 '19 at 20:40
  • 1
    You cannot tell that the program still works when `rst[i] = '\0';` is left out. You can run the program without that line, and it might be okay that time you run it, but that does not prove the program will work all the time, especially if the code is changed in other ways or it is compiled with a different compiler or options. In one run, the program might just happen to “work” because there is already a zero in the necessary place in `rst` already. But that is not guaranteed to be always true, so the statement `rst[i] = '\0';` is needed. – Eric Postpischil Sep 29 '19 at 20:40
  • Thank you, Mr. @EricPostpischil. This helps! But, may I add on to the question? Why has the null character been mentioned for just the one string, and not the other? – Bao Sep 29 '19 at 20:51
  • 1
    The other it’s already there. You are building the new st string only. – Daniel Tran Sep 29 '19 at 20:52
  • add a `memset(rst, 'x', sizeof(rst))` after your declaration of `rst`, to simulate unexpected garbage in uninitialized 'rst', then you should see the difference of adding `\0` or not. – Adrian Shum Sep 30 '19 at 02:05

1 Answers1

1

Including null character guarantees the second string will end with a null character so the result would be correct.

Even though there is chance that the second string will first be initiate with all zero on it, that means you don’t have to assign the null character. But normally you can’t predict what is the content of the st string when you have just allocated the memory.

Daniel Tran
  • 6,083
  • 12
  • 25
  • Sir, could you kindly cite an example of such a string that will initiate to zero in reverse? I'd be highly obliged. – Bao Sep 29 '19 at 21:22
  • Bao, that depends on the compiler and/or operating system. The very first line of the program allocates memory for `rst`. The contents of this array, in memory, before you write to it, are not guaranteed to be null bytes (null characters). It will most likely be random bytes. So it's not a matter of particular input strings not requiring the null character after copying the characters in reverse. – GordonAitchJay Sep 30 '19 at 08:19