Write a C program demonstrating strlen library function

CServer Side ProgrammingProgramming

The strlen () function

It returns the number of characters in a string.


int strlen (string name)

In this program, with the help of gets function reading the name at run time and trying to print the length of that name using strlen() function, this function returns an integer value and try to print that no using printf.

Example 1

 Live Demo

void main(){
   //Declaring string and length//
   char name[25];
   int length;
   //Reading Input from user//
   printf("Enter your name : ");
   //Printing name//
   printf("Your name is : ");
   printf("Length of the string is : %d\n",length);


Enter your name : Tutorialspoint
Your name is : Tutorialspoint
Length of the string is : 14

We shall consider another example to print string length without using string function i.e., without using strlen().

Example 2

 Live Demo

#include <stdio.h>
int main(){
   char string[50],i;
   printf("enter the string: \n");
   for(i=0; string[i]!='\0'; ++i);
      printf("\length of the given string is: %d",i);
   return 0;


enter the string:
length of the given string is: 14
Published on 08-Mar-2021 18:51:05