Introduction
Finding the length of a string involves counting the number of characters in the string, excluding the null terminator (\0). This guide will show you how to write a C program to find the length of a string provided by the user.
Problem Statement
Create a C program that:
- Takes a string as input from the user.
- Calculates the length of the string.
- Displays the length of the string.
Example:
- Input: String = "Hello"
- Output: Length = 5
Solution Steps
- Include the Standard Input-Output and String Libraries: Use
#include <stdio.h>for standard input-output functions and#include <string.h>for string functions. - Write the Main Function: Define the
mainfunction, which is the entry point of every C program. - Declare Variables: Declare a variable to store the string and a variable to store the length of the string.
- Input the String: Use
getsorscanfto take input from the user for the string. - Find the Length of the String: Use the
strlenfunction from thestring.hlibrary to calculate the length of the string. - Display the Length of the String: Use
printfto display the length of the string.
C Program Using strlen Function
#include <stdio.h>
#include <string.h>
int main() {
// Step 1: Declare a variable to hold the string and another for the length
char str[100];
int length;
// Step 2: Prompt the user to enter a string
printf("Enter a string: ");
gets(str); // Using gets to read the string including spaces
// Step 3: Find the length of the string using strlen
length = strlen(str);
// Step 4: Display the length of the string
printf("Length of the string \"%s\" is: %d\n", str, length);
return 0; // Step 5: Return 0 to indicate successful execution
}
C Program Without Using strlen Function
Alternatively, you can find the length of the string manually by iterating through the string until you reach the null terminator (\0).
#include <stdio.h>
int main() {
// Step 1: Declare a variable to hold the string and another for the length
char str[100];
int length = 0;
// Step 2: Prompt the user to enter a string
printf("Enter a string: ");
gets(str); // Using gets to read the string including spaces
// Step 3: Find the length of the string manually
while (str[length] != '\0') {
length++;
}
// Step 4: Display the length of the string
printf("Length of the string \"%s\" is: %d\n", str, length);
return 0; // Step 5: Return 0 to indicate successful execution
}
Explanation
Using strlen Function
- The
strlenfunction from thestring.hlibrary calculates the length of the string by counting the number of characters before the null terminator (\0).
Without Using strlen Function
- The program manually calculates the length of the string by iterating through each character of the string until it encounters the null terminator (
\0). The variablelengthis incremented during each iteration to keep track of the number of characters.
Steps Explanation:
- Declare Variables: The
strarray is declared to store the input string, andlengthis used to store the length of the string. - Input the String: The
getsfunction reads the input string, including spaces, and stores it instr. - Calculate the Length: Depending on the approach, either the
strlenfunction or a manual loop calculates the length of the string. - Display the Length: The program displays the calculated length of the string using
printf.
Output Example
Example:
Enter a string: Hello
Length of the string "Hello" is: 5
Another Example:
Enter a string: C Programming
Length of the string "C Programming" is: 13
Conclusion
This C program demonstrates two methods to find the length of a string: using the strlen function and manually iterating through the string. It covers basic concepts such as string manipulation, loops, and functions, making it a useful example for beginners learning C programming.