C program to find the length of a string
Given a string str. The task is to find the length of the string.

Examples:
Input: str = "Geeks" Output: Length of Str is : 4 Input: str = "GeeksforGeeks" Output: Length of Str is : 13
In the below program, to find the length of the string str, first the string is taken as input from the user using scanf in
, and then the length of Str is calculated using
and using
method .
Below is the C program to find the length of the string.
Example 1: Using loop to calculate the length of string.
// C program to find the length of string #include <stdio.h> #include <string.h> int main() { char Str[1000]; int i; printf("Enter the String: "); scanf("%s", Str); for (i = 0; Str[i] != '\0'; ++i); printf("Length of Str is %d", i); return 0; } |
Enter the String: Geeks Length of Str is 5
Example 2: Using strlen() to find the length of the string.
// C program to find the length of // string using strlen function #include <stdio.h> #include <string.h> int main() { char Str[1000]; int i; printf("Enter the String: "); scanf("%s", Str); printf("Length of Str is %ld", strlen(Str)); return 0; } |
Enter the String: Geeks Length of Str is 5
Recommended Posts:
- Lex program to find the Length of a String
- Program to find Length of Bridge using Speed and Length of Train
- How to find length of a string without string.h and loop in C?
- C program to print the length of a String using %n format specifier
- Python program to print even length words in a string
- Find longest length number in a string
- Find number of magical pairs of string of length L
- Lex program to find the length of the longest word
- The length of a string using pointers
- Counts of distinct consecutive sub-string of length two using C++ STL
- Program for focal length of a lens
- C Program for focal length of a spherical mirror
- Reduce string to shortest length by deleting a pair of same adjacent characters
- Program to determine focal length of a spherical mirror
- C program to print a string without any quote (singe or double) in the program
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



