![]() |
VOOZH | about |
The length of a string is the number of characters in it without including the null character (‘\0’). In this article, we will learn how to find the length of a string in C.
The easiest way to find the string length is by using strlen() function from the C strings library. Let's take a look at an example:
5
There are also a few other methods in C to find the length of a string. They are as follows:
Table of Content
Traverse the string using a loop from the first character till the null terminator ('\0') is encountered while counting the number of characters.
5
Increment the pointer to the string array (different from the pointer to the first element of the string), dereference it and subtract the pointer to the first character of the string.
5
Note: This method can only be used when the string is declared as character array and only inside the same scope as string is declared.
Use pointer subtraction to find the difference between the pointer to the first and last character of the string, calculating the length excluding the null terminator.
5