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:
- 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?
- Python program to print even length words in a string
- Find longest length number in a string
- The length of a string using pointers
- Counts of distinct consecutive sub-string of length two using C++ STL
- Reduce string to shortest length by deleting a pair of same adjacent characters
- Program for focal length of a lens
- C Program for focal length of a spherical mirror
- Program to determine focal length of a spherical mirror
- C Program for Number of jump required of given length to reach a point of form (d, 0) from origin in 2D plane
- C program to print a string without any quote (singe or double) in the program
- Find if a string starts and ends with another given string
- C Program for Program to find area of a circle
- Program to Reverse a String using Pointers
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.



