strlen() Function in C: Finding String Length

Rumman Ansari   Software Engineer   2024-07-05 04:22:12   8369  Share
Subject Syllabus DetailsSubject Details 2 Program
☰ TContent
☰Fullscreen

Table of Content:

strlen( ) function in C gives the length of the given string. Syntax for strlen( ) function is given below.

Syntax

size_t strlen(const char *str);

Returns

The strlen( ) function returns the length of the null-terminated string pointed to by str. The null terminator is not counted.

Program

In below example program, length of the string "ILoveCProgrammingLanguage" is determined by strlen( ) function as below. Length of this string 25 is displayed as output.

#include 
#include 
 
int main( )
{
    int length;
    char array[50]="ILoveCProgrammingLanguage" ;
 
    length = strlen(array) ;
 
    printf ( "\string length  = %d \n" , length ) ;
    return 0;
}

Output

string length  = 25
Press any key to continue . . .
#include 
#include  
 
int main( )
{ 
    printf("%d \n", strlen("ILoveCProgrammingLanguage"));
    return 0;
}

Output

25
Press any key to continue . . .

Points to be Noted

  1. strlen( ) function counts the number of characters in a given string and returns the integer value.
  2. It stops counting the character when a null character is found. Because null character indicates the end of the string in C.