![]() |
VOOZH | about |
C language provides a built-in function strlen() but in this article, we will learn how to find and print the length of the string using pointers in C.
The easiest method to find the length of a string using pointers is by calculating difference between the pointers to the first and last character of the string. Let's take a look at an example:
5
There are two other methods in C to find the length of the string using pointers:
Increment the pointer to the string till it points to the null character while keeping the count of characters encountered.
5
This technique is only useful when the string is declared as character array and is declared in the same scope. 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