C strlen()

The strlen() function calculates the length of a given string.

C strlen() Prototype

size_t strlen(const char *str);

The function takes a single argument, i.e, the string variable whose length is to be found, and returns the length of the string passed.

The strlen() function is defined in <string.h> header file.

calculation of string using strlen() library function.

Example: C strlen() function

#include <stdio.h>
#include <string.h>
int main()
{
    char a[20]="Program";
    char b[20]={'P','r','o','g','r','a','m','\0'};
    char c[20];

    printf("Enter string: ");
    gets(c);

    printf("Length of string a = %d \n",strlen(a));

    //calculates the length of string before null charcter.
    printf("Length of string b = %d \n",strlen(b));
    printf("Length of string c = %d \n",strlen(c));

    return 0;
}

Output

Enter string: String
Length of string a = 7
Length of string b = 7
Length of string c = 6