The strlen () function
It returns the number of characters in a string.
Syntax
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
#include<stdio.h> #include<string.h> void main(){ //Declaring string and length// char name[25]; int length; //Reading Input from user// printf("Enter your name : "); gets(name); length=strlen(name); //Printing name// printf("Your name is : "); puts(name); printf("Length of the string is : %d\n",length); }
Output
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
#include <stdio.h> int main(){ char string[50],i; printf("enter the string: \n"); scanf("%s",string); for(i=0; string[i]!='\0'; ++i); printf("\length of the given string is: %d",i); return 0; }
Output
enter the string: TutorialsPoint length of the given string is: 14