OverIQ.com

The strlen() Function in C

Last updated on July 27, 2020


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.

The 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:

1
2
3
4
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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#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:

1
2
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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#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:

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

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