Rewrite C Function: strlen

Last Updated : 10 July, 2022 · 2 min read

articles banner image

In this article we will take a closer look at strlen function in C programming language. With this text we are aiming to satisfy the following four objectives:

Structure of strlen:

Declaration: size_t strlen(const char *str);

Return: number of characters within const char *str (not including '\0').

Parameter list:
(const char *str) - an array to be checked for the character count.

Header file: <string.h>.

1. So, what is strlen?

Strlen is a function that returns the length of a string. It takes in a const char * as a parameter. Then counts existing characters inside a string and returns the resulting number. This number is, in turn, the length of a given string.

Note: strlen does not count a string terminating character ('\0').

If we were to provide strlen with an argument "learn", for example, the function would return us the length of 5. Even though string literal of "learn" has 6 characters (l, e, a, r, n, '\0').

Function strlen is defined in <string.h> library, which means that in order to use this function we would need to include this header file into our program (e.g. #include <string.h>).

Example 1: Usage of the strlen function in C

# include <string.h>

int main ( void )
{
    
strlen ( "learn" ); //returns 5
    
return 0 ;
}

2. And how to rewrite strlen function in C?

Consider the code snippet below for a second:

Example 2: Rewritten strlen function in C.

unsigned int new_strlen ( const char *str )
{
    
unsigned int len = 0 ;
    
while ( str[len] != '\0' )
      
len++;
    
return len;
}

Here, we define a new_strlen function that takes a constant string pointer (const str *) as a parameter and returns an unsigned integer.

Inside this functions body, we define an unsigned integer type variable called len and give it a value of 0. Inside the while loop** we use this len variable to iterate through the received str constant string and increment** len for each existing character until we encounter the string terminating one ('\0').

Lastly, we return the resulting value of len.

Now, if we were to pass the same "learn" string literal as an argument to this function (e.g. new_strlen("learn")), we would again get the result of 5.

3. So, what are the limitations of strlen function?

It could be argued, that strlen function has two main limitations.

For one, it can only handle null terminated strings. If the string passed as an argument to strlen function is not null terminated, then behaviour of this function is undefined. This means that strings defined in such a way: char s2[3] = { '1','2','3'}; would cause an undefined behaviour when passed as an argument.

Even if programmer does not see this as a limitation, it is, nontheless, important to keep this in mind while writing programs in C programming language.

For two, passing a NULL pointer as an argument (e.g. strlen(NULL)) causes a segmentation fault**. This means that inside the body of this library function it is not checked for NULL before the pointer is being accessed.

As a way to counter that in our new_strlen function, we could replace line 4 with the following code:

while(str != NULL && str[len] != '\0')

This way passing a NULL pointer to the new_strlen function would not cause a segmentation fault and would return 0. Which may or may not be optimal depending on the architecture of your program.

4. Quiz of strlen knowledge

Quiz : Rewritten strlen function in C

1What would strlen return, if strlen("Hello world");?

2Strlen counts terminating NULL character.

3Passing a NULL pointer to strlen...

quiz completed!

Congrats!

Feel free to share your achievement below!