Find the Length of a String without using strlen()
To find the length of a string in C without using the strlen() function, we can iterate through the string character by character until we reach the null character '\0'. The number of iterations gives us the length of the string. Below, we will explore multiple ways to achieve this using loops and recursion.
Examples to Find String Length
1. Finding String Length Using a for Loop
In this example, we use a for loop to traverse the string and count the number of characters before encountering the null character '\0'.
main.c
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
int length = 0;
// Using a for loop to count characters
for (int i = 0; str[i] != '\0'; i++) {
length++;
}
printf("Length of the string: %d\n", length);
return 0;
}
Explanation:
- We declare a character array
str[]and initialize it with “Hello, World!”. - We initialize an integer variable
lengthto 0, which will store the count of characters. - We use a
forloop to iterate through the string. - The loop runs until it encounters the null character
'\0', which marks the end of the string. - In each iteration,
lengthis incremented. - Finally, we print the length of the string.
Output:
Length of the string: 13
2. Finding String Length Using a while Loop
In this example, we use a while loop to traverse the string until we reach the null character.
main.c
#include <stdio.h>
int main() {
char str[] = "C Programming";
int length = 0;
int i = 0;
// Using a while loop to count characters
while (str[i] != '\0') {
length++;
i++;
}
printf("Length of the string: %d\n", length);
return 0;
}
Explanation:
- We declare a character array
str[]initialized with “C Programming”. - We initialize two integer variables:
length(set to 0) to store the count andi(set to 0) to traverse the string. - The
whileloop runs as long asstr[i] != '\0'. - In each iteration,
lengthis incremented, andimoves to the next character. - When the loop exits, we print the calculated length.
Output:
Length of the string: 13
3. Finding String Length Using Recursion
In this example, we use recursion to count the number of characters in a string.
main.c
#include <stdio.h>
// Recursive function to calculate string length
int findLength(char str[], int index) {
if (str[index] == '\0')
return 0;
return 1 + findLength(str, index + 1);
}
int main() {
char str[] = "Arjun";
int length = findLength(str, 0);
printf("Length of the string: %d\n", length);
return 0;
}
Explanation:
- We define a recursive function
findLength()that takes the string and an index as arguments. - The base case checks if
str[index] == '\0'. If true, the function returns 0. - Otherwise, it returns 1 plus the result of the function called with the next index.
- In
main(), we callfindLength()with the string and index0. - We print the length returned by the function.
Output:
Length of the string: 5
Conclusion
We explored multiple ways to find the length of a string without using strlen():
forLoop: Iterates through the string and counts characters.whileLoop: Uses a condition to count until'\0'is reached.- Recursion: Calls itself to count characters one by one.
Each method has its own use case, and recursion is particularly useful for functional programming approaches.
