In this article, we provide a comprehensive guide to finding the length of a string in C. You’ll find a clear algorithm, the complete C program, and a step-by-step explanation of each part of the process. This guide is perfect for beginners and intermediate programmers looking to understand string manipulation in C.
Method 1: Using fgets from the standard input-output library
Algorithm to Find the Length of a String
Step 1: Start
Step 2: Input: Read the string from the user.
Step 3: Initialize Counter: Set the counter to 0.
Step 4: Iterate Through the String:
(i) Loop through each character of the string until the null
character ('\0') is encountered.
(ii) Increment the counter for each character.
Step 5: Output: Display the counter as the length of the string.
Step 6: End
C Program to Find the Length of a String
#include <stdio.h>
int main() {
char str[100];
int length = 0;
// Step 2: Input the string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
// Step 4: Iterate through the string to calculate its length
while (str[length] != '\0') {
length++;
}
// If the last character is a newline (due to fgets), decrement the length
if (str[length - 1] == '\n') {
length--;
}
// Step 5: Output the length of the string
printf("Length of the string is: %d\n", length);
// Step 6: End
return 0;
}
Explanation of Each Step
Include Header:
#include <stdio.h>
- This line includes the standard input-output library for using functions like printf and fgets.
Input the String:
char str[100];
int length = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
- These lines declare a character array to store the string and an integer to count the length.
- The fgets function reads a line of text from the standard input and stores it in str.
Initialize Counter:
int length = 0;
- This line initializes the counter length to 0.
Iterate through the string:
while (str[length] != '\0') {
length++;
}
if (str[length - 1] == '\n') {
length--;
}
- The while loop iterates through each character in the string until it encounters the null character (‘\0’), incrementing the counter length for each character.
- If the last character is a newline (because fgets includes the newline character), the length is decremented by 1 to exclude it.
Output of the Length of the String:
printf("Length of the string is: %d\n", length);
- This line prints the calculated length of the string.
End:
return 0;
- This line indicates the successful completion of the program.
Summary
This program efficiently calculates and prints the length of a string input by the user. It uses basic string handling techniques in C, demonstrating how to iterate through a string and count its characters.
Method 2: Using the ‘strlen’ function
Using the strlen function from the C standard library, we can simplify the program even further. The strlen function calculates the length of a string excluding the null terminator (‘\0’).
Simplified Algorithm to Find the Length of a String Using strlen
Step 1: Start
Step 2: Input: Read the string from the user.
Step 3: Calculate Length: Use the strlen function to determine
the length of the string.
Step 4: Output: Display the length of the string.
Step 5: End
C Program to Find the Length of a String Using strlen
#include <stdio.h>
#include <string.h> // Include the string.h library for strlen
int main() {
char str[100];
// Step 2: Input the string
printf("Enter a string: ");
scanf("%s", str);
// Step 3: Calculate the length using strlen
int length = strlen(str);
// Step 4: Output the length of the string
printf("Length of the string is: %d\n", length);
// Step 5: End
return 0;
}
Explanation of Each Step
Include Header Files:
#include <stdio.h>
#include <string.h>
These lines include the standard input-output library for using functions like printf and scanf, and the string library for using strlen.
Input the String:
char str[100];
printf("Enter a string: ");
scanf("%s", str);
- These lines declare a character array to store the string and use scanf to read a word (a sequence of characters without spaces) from the standard input and store it in str.
Calculate Length Using strlen:
int length = strlen(str);
- The strlen function is called to calculate the length of the string (excluding the null terminator).
Output the Length of the String:
printf("Length of the string is: %d\n", length);
- This line prints the calculated length of the string.
End:
return 0;
Summary
This simplified program uses the strlen function to efficiently calculate and print the length of a string input by the user, using scanf to read the string. Note that scanf stops reading input at the first whitespace character, so it is suitable for reading a single word. If you need to handle spaces in the input, you would need a different approach, such as using gets or fgets with manual handling of the newline character.
Discover more from lounge coder
Subscribe to get the latest posts sent to your email.