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 without using any library functions like strlen.
Problem Statement
Create a C program that:
- Takes a string as input from the user.
- Calculates the length of the string manually.
- Displays the length of the string.
Example:
- Input: String = "Hello"
- Output: Length = 5
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>for standard input-output 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 count the length of the string.
- Input the String: Use
getsorscanfto take input from the user for the string. - Calculate the Length of the String Manually: Use a loop to iterate through the string until the null terminator is encountered, counting each character.
- Display the Length of the String: Use
printfto display the length of the string.
C Program to Find Length of String Without Using Library Function
#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
Step 1: Declare Variables
- The variable
stris declared to store the input string. The variablelengthis used to count the number of characters in the string.
Step 2: Input the String
- The program prompts the user to enter a string using
printf. Thegetsfunction is used to read the string, allowing it to include spaces.
Step 3: Calculate the Length of the String
- The program uses a
whileloop to iterate through each character of the string until it encounters the null terminator (\0). Thelengthvariable is incremented during each iteration to count the number of characters.
Step 4: Display the Length of the String
- After calculating the length, the program displays the length of the string using the
printffunction.
Step 5: Return 0
- The
return 0;statement indicates that the program executed successfully.
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: 14
Conclusion
This C program demonstrates how to find the length of a string manually by iterating through each character until the null terminator is reached. It covers basic concepts such as string manipulation, loops, and character counting, making it a useful example for beginners learning C programming.