strlen() function in c

The strlen() function calculates the length of a given string.The strlen() function is defined in string.h header file. It doesn’t count null character ‘\0’.

Syntax:

int strlen(const char *str);

Parameter:

Return: This function returns the length of string passed.

Below programs illustrate the strlen() function in C:



Example 1:-

filter_none

edit
close

play_arrow

link
brightness_4
code

// c program to demonstrate
// example of strlen() function.
  
#include<stdio.h>
#include <string.h>
  
int main()
{
   char ch[]={'g', 'e', 'e', 'k', 's', '\0'};
  
   printf("Length of string is: %d", strlen(ch));
  
 return 0;
}
chevron_right

Output :

Length of string is: 5

Example 2:-

filter_none

edit
close

play_arrow

link
brightness_4
code

// c program to demonstrate
// example of strlen() function.
  
#include<stdio.h>
#include <string.h>
  
int main()
{
   char str[]= "geeks";
  
   printf("Length of string is: %d", strlen(str));
  
 return 0;
}
chevron_right

Output:

Length of string is: 5

Example 3:-

filter_none

edit
close

play_arrow

link
brightness_4
code

// c program to demonstrate
// example of strlen() function.
#include<stdio.h>
#include <string.h>
  
int main()
{
   char *str = "geeks";
  
   printf("Length of string is: %d", strlen(str));
  
 return 0;
}
chevron_right

Output :

Length of string is: 5

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
C
Practice Tags :