The strlen() function in C

This section discusses some commonly used functions provided by string library in C. These functions are declared in the header file string.h, so before using these function you must include string.h in your program.

#include<string.h>

The strlen() function

Syntax: size_t strlen (const char* str);

Note: For this chapter ignore the keyword const. It will be discussed later.

strlen() accepts an argument of type pointer to char or (char*), so you can either pass a string literal or an array of characters. It returns the number of characters in the string excluding the null character '\0'. Recall that size_t is just an alias of unsigned int.

Here are some examples:

strlen("a string constant"); // returns 17

char arr[] = "an array of characters";
strlen(arr); // returns 22

The following program calculates the length of the string entered by the user.

#include<stdio.h>
#include<string.h>

int main()
{
    int len;

    // destination array can store only 30 characters including '\0'
    char destination[30];

    printf("Enter your dream destination: ");
    gets(destination);

    // calculate length of characters in destination
    len = strlen(destination); 

    printf("Your dream destination %s has %d characters in it", destination, len);

    // signal to operating system program ran fine
    return 0;
}

Expected Output:

Enter your dream destination: Bermuda Triangle
Your dream destination Bermuda Triangle has 16 characters in it

We have a strong foundation on how string works in general. So let's create our own version of the strlen() function.

unsigned int my_strlen(char *p)
{
    unsigned int count = 0;

    while(*p!='\0')
    {
        count++;
        p++;
    }

    return count;
}

How it works

Just like original strlen() function, this one accepts an argument of type pointer to char or (char*) and returns unsigned int. Inside the function, we have declared a variable count and initialized it to 0. The while loop is used to count the number of characters. After each iteration, p is incremented by 1. When p points to the address of null character ('\0') the loop stops and the value of the variable count is returned to the calling function.

Let's rewrite our previous program, incorporating the definition of my_strlen() function.

#include<stdio.h>
unsigned int my_strlen(char *p); // function declaration

int main()
{
    int len;

    // destination array can store only 30 characters including '\0'
    char destination[30];

    printf("Enter your dream destination: ");
    gets(destination);
    
    // calculate length of characters in destination
    len = my_strlen(destination);

    printf("Your dream destination %s has %d characters in it", destination, len);

    // signal to operating system program ran fine
    return 0;
}

// definition of my_strlen() function

unsigned int my_strlen(char *p)
{
    unsigned int count = 0;

    while(*p!='\0')
    {
        count++;
        p++;
    }
    return count;
}

Expected Output:

Enter your dream destination: Bermuda Triangle
Your dream destination Bermuda Triangle has 16 characters in it

my_strlen() function is giving the same output as strlen() function, so our function is working as expected.