Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does C know the end of my string?

Tags:

c

string

I have a program in which I wanted to remove the spaces from a string. I wanted to find an elegant way to do so, so I found the following (I've changed it a little so it could be better readable) code in a forum:

char* line_remove_spaces (char* line)
{
    char *non_spaced = line;
    int i;
    int j = 0;
    for (i = 0; i <= strlen(line); i++)
    {
        if ( line[i] != ' ' )
        {
            non_spaced[j] = line[i];
            j++;
        }
    }
    return non_spaced;
}

As you can see, the function takes a string and, using the same allocated memory space, selects only the non-spaced characters. It works!

Anyway, according to Wikipedia, a string in C is a "Null-terminated string". I always thought this way and everything was good. But the problem is: we put no "null-character" in the end of the non_spaced string. And somehow the compiler knows that it ends at the last character changed by the "non_spaced" string. How does it know?

like image 489
vaulttech Avatar asked Apr 27 '12 11:04

vaulttech


People also ask

How do you determine the end of a string?

The endsWith() method determines whether a string ends with the characters of another string, returning true or false as appropriate.

How do you check if you have reached the end of a string in C?

The strrchr() function finds the last occurrence of c (converted to a character) in string . The ending null character is considered part of the string . The strrchr() function returns a pointer to the last occurrence of c in string . If the given character is not found, a NULL pointer is returned.

How does C handle strings?

Overview. 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 arrays of chars, terminated by a null byte.

Which character indicates end of string in C?

The null character indicates the end of the string. Such strings are called null-terminated strings.


2 Answers

This does not happen by magic. You have in your code:

for (i = 0; i <= strlen(line); i++)
              ^^

The loop index i runs till strlen(line) and at this index there is a nul character in the character array and this gets copied as well. As a result your end result has nul character at the desired index.

If you had

for (i = 0; i < strlen(line); i++)
              ^^

then you had to put the nul character manually as:

for (i = 0; i < strlen(line); i++)
{
    if ( line[i] != ' ' )
    {
        non_spaced[j] = line[i];
        j++;
    }
}
// put nul character
line[j] = 0;
like image 66
codaddict Avatar answered Nov 09 '22 21:11

codaddict


Others have answered your question already, but here is a faster, and perhaps clearer version of the same code:

void line_remove_spaces (char* line)
{
  char* non_spaced = line;

  while(*line != '\0')
  {
    if(*line != ' ')
    {
      *non_spaced = *line;
      non_spaced++;
    }

    line++;
  }

  *non_spaced = '\0';
}
like image 36
Lundin Avatar answered Nov 09 '22 19:11

Lundin