Skip to content
Related Articles

Related Articles

strlen() function in c
  • Difficulty Level : Basic
  • Last Updated : 28 Sep, 2018

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:

  • str: It represents the string variable whose length we have to find.

Return: This function returns the length of string passed.

Below programs illustrate the strlen() function in C:



Example 1:-




// 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;
}


Output :

Length of string is: 5

Example 2:-




// 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;
}


Output:

Length of string is: 5

Example 3:-




// 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;
}


Output :

Length of string is: 5

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :