C strlen() function to get the string length

strlen( ) function definition – In C, strlen( ) function is used to calculate the length of the string. It means that it counts the total number of characters present in the string which includes alphabets, numbers, and all special characters including blank spaces. It’s syntax is as follows –

    n = strlen(string);

Here ‘n‘ is an integer variable, which receives the values of the length of the string. The argument may be a string constant also. The counting ends at the first null character.
Example of strlen() function in C is –

   nm = study street;
   n = strlen(nm);
   n=13;

Sample program of C, strlen( ) function to calculate string length C

/* Sample C strlen() program */

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
	char nm[20];
	int length;
    
    clrscr();
    printf("Enter your name");
    gets(nm);
    
    length=strlen(nm);   /* will feed the lenght of the string in variable */
    
    printf("Length of the entered string is %d", length);
    getch();
}

Input
Enter a string – study street

Output
Length of the string is 12

yashan has written 70 articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>