Count the Number of Characters Using StrLen Function in C

In C language, String Length or strlen() function calculates the number of character with one argument and returns the value of type integer.

strlen function defined in string.h header file. In this tutorial we are going to explain how to use this function by applying the function into code.

First, you need to include the two header file, the standard input/output and string..

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

int main()

{




return 0;
}
Second, declare a variable array with char data type and an integer type variable.

char str[100];
int len;

Third, ask the user input a string.


printf("\nEnter any String: ");

Fourth, we will use another function called gets
What is gets function?

It is used to scan a line of text from a standard input device. The gets() function will be terminated by newline character. The newline character won't be included as part of the string. The string may include white space characters.

gets(str);

followed by displaying the output

len=strlen(str);
printf("\nNumber of Character in %s = %d\n",str,len);
Whole Code:

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

int main()
{
char str[100];
int len;
printf("\nEnter any String: ");
gets(str);
len=strlen(str);
printf("\nNumber of Character in %s = %d\n",str,len);
return 0;
}

Final Output:


Count the Number of Characters Using StrLen Function in C Count the Number of Characters Using StrLen Function in C Reviewed by code-dev on 12:25 PM Rating: 5

No comments:

Powered by Blogger.