O'Reilly logo

C Programming: Visual Quickstart Guide by Marc Liyanage, Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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)); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required