C Program to Find the Length of a String
Write a C program to find the Length of String using loop
- Takes a string input from the user using gets().
- Initializes a counter length to 0 for tracking string length.
- Increments length in a loop until the null terminator ('\0') is reached.
- Prints the total number of characters as the string length.
Example : pgm.c
#include <stdio.h> int main() { char str[100]; int length = 0; printf("Enter a string: "); gets(str); // Count characters until null terminator is found while (str[length] != '\0') { length++; } printf("Length of the string is: %d\n", length); return 0; }
Output:
Enter a string: Welcome
Length of the string is: 7
Length of the string is: 7