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

Hey! Looking for some great resources suitable for young ones? You've come to the right place. Check out our self-paced courses designed for students of grades I-XII.
Start with topics like Python, HTML, ML, and learn to make some games and apps all with the help of our expertly designed content! So students worry no more, because GeeksforGeeks School is now here!
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

