String Length Using strlen() in C
On this page (7sections)
About this program
This is an example program in c string example programs. Read the concept first: C Strings in C Programming, then study the code and output below.
Definition:
- strlen() is a default function in C programming which can be used to find the length of a string.
- The input to this function should be the character array of the string whose length is to be found.
Syntax of strlen
size =strlen(String_variable);
size is int.
String_variable is char array
Example Program for Length Of String using strlen()
/*##Length Of String using strlen() In C Programing */
/*##String Programs, Datatype Programs, Basic Programs*/
/*##Example*/
// Coded By GJRS
#include <stdio.h>
#include <string.h>
int main()
{
char charArray[100];
int length;
printf("Enter the string whose length is to be found : ");
gets(charArray);
length = strlen(charArray);
printf("Length of the string is : %d",length);
return 0;
}
Output:
Enter the string whose length is to be found : Programming
Length of the string is : 11
Related Pages
Learn the concept first, then study the code:
- C Programs — Browse all C Programs.
- C Strings in C Programming — Concept — char arrays, strlen, strcpy and strcmp.
- Concatenation of string C Example Program — More in c string example programs.
- Swapping Two String using strcpy In C Programing — More in c string example programs.
Frequently Asked Questions
What does this C program do?
It is a C example program that demonstrates String Length Using strlen(), including the complete source code and the expected sample output.
How do I compile and run this C program?
Save the code in a `.c` file, compile it with `gcc filename.c -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses arrays and reading user input, illustrating a common pattern in C programming.