Finding the Length of a String

In the previous example, the sizeof() operator was used to determine the memory requirements of a variable. But what if you wanted to just count the number of characters in a string? For that, you can use the appropriately named strlen() function (short for string length):

num = strlen(my_string);

Note that this function does not count the terminating \0 character that concludes every string. For example (Figure 11.5):

char my_string[] = "Larry";

printf("The string '%s' has %d
 characters.", my_string,
 strlen(my_string)); ...

Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.