Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is with the null character in reverse string program?

I had to make a simple program to reverse a string. I eventually got this code from my own understanding but also help from google since I couldn't originally get it to work. It runs fine and outputs as it should and I understand all of it except for the reverse[j] = '\0' statement. I kept getting symbols in my output when I didn't state it but I want to know how this works. Can anyone explain please?

#include<stdio.h>

int main(void)
{
    char original[20], reverse[20];
    int length, i, j;

    printf("Enter a string:\n");
    gets(original); 

    length = strlen(original);

    for (i = length - 1, j= 0; i >= 0; i--, j++)
        reverse[j] = original[i];

    reverse[j] = '\0';  //I don't know what this statement does exactly

    printf("The string reversed is:\n %s\n", reverse);

    return 0;
}
like image 811
Mrs.Brightside Avatar asked Dec 07 '25 07:12

Mrs.Brightside


2 Answers

If you want that a character array would contain a string then it has to have the terminating zero.

For example function strlen that is used in your program counts characters in a character array before the terminating zero.

Also function printf used with the format specifier %s outputs characters from a character array until the terminating zero will be encountered.

For example if you have an array like this

char s[10] = "Hello";

then the call strlen( s ) returns 5 instead of 10. And call printf( "%s\n", s ); outputs 6 characters (including the new line character).

Consider this demonstrative program

#include <stdio.h>

int main(void) 
{
    char s[10] = "Hello";

    printf( "%d\n", printf( "%s\n", s ) );

    return 0;
}

Its output is

Hello
6

This initialization

char s[10] = "Hello";

is fully equivalent to

char s[10] = { 'H', 'e', 'l', 'l', 'o', '\0', '\0', '\0', '\0', '\0'};

If you need to reverse the string stored in the array it is obvious that you need to reverse characters before the first terminating zero. And if you want to copy the string in the reversed order to another character array you need to append the destination array with the terminating zero.

This loop

for (i = length - 1, j= 0; i >= 0; i--, j++)
    reverse[j] = original[i];

copies in the reversed order all characters except the terminating zero from the original character array starting with the last character before the terminating zero to the destination character array. You need to append the destination character array with the terminating zero

reverse[j] = '\0';
like image 156
Vlad from Moscow Avatar answered Dec 09 '25 23:12

Vlad from Moscow


The \0 is not really a character. It's the way a c program mark the end of a string. So if you only want to reverse a string you don't have to move this character at the begining of the reversed string.

original string: hello\0
reverse string : olleh\0
like image 24
Dareg Avatar answered Dec 09 '25 21:12

Dareg