C program to calculate length of string using strlen() function

Ex: Write a C program to calculate length of string using strlen() function. How to calculate length of string using strlen() function. C program to calculate length of string using strlen() function.

Input from user:


Enter the string:
This is codeforhunger

Expected output:

Length of string is: 21





  Step by step logic of the given program:

1. Accept string from user store it in variable say a.


2. After that calculate length of string using strlen() function and store it in variable length.

3. Print length on the output screen.




  Program to calculate length of string using strlen() function:



#include<stdio.h>
#include<string.h>

int main()
{
char a[100];
int length;

printf("Enter the string:\n");
    gets(a);
    
    length=strlen(a);//calculate length of string

printf("Length of string is: %d",length);

return 0;
}



Above program shows the following output:


C program to calculate length of string using strlen() function

No comments:

Post a Comment

If you have any doubts, please discuss here...👇