C Program to Find String Length Without strlen Function
strlen by looping through each character until you reach the null character '\0', counting each step with a variable like length.Examples
How to Think About It
strlen, start at the first character and move forward one by one, counting each character until you find the special end marker '\0' that shows the string ends.Algorithm
Code
#include <stdio.h> int main() { char str[100]; int length = 0; printf("Enter a string: "); fgets(str, sizeof(str), stdin); while (str[length] != '\0' && str[length] != '\n') { length++; } printf("Length of string is %d\n", length); return 0; }
Dry Run
Let's trace the input "Hello" through the code to find its length.
Initialize length
length = 0
Check first character
str[0] = 'H' (not '\0' or '\n'), length = 1
Check second character
str[1] = 'e' (not '\0' or '\n'), length = 2
Check third character
str[2] = 'l' (not '\0' or '\n'), length = 3
Check fourth character
str[3] = 'l' (not '\0' or '\n'), length = 4
Check fifth character
str[4] = 'o' (not '\0' or '\n'), length = 5
Check sixth character
str[5] = '\n' (stop counting)
| Index | Character | Length |
|---|---|---|
| 0 | H | 1 |
| 1 | e | 2 |
| 2 | l | 3 |
| 3 | l | 4 |
| 4 | o | 5 |
| 5 | \n | stop |
Why This Works
Step 1: Start counting characters
We begin with a counter at zero and check each character in the string one by one.
Step 2: Stop at string end
The loop stops when it finds the null character '\0' or newline '\n', which marks the string's end.
Step 3: Return the count
The counter now holds the total number of characters before the end marker, which is the string length.
Alternative Approaches
#include <stdio.h> int main() { char str[100]; char *ptr; int length = 0; printf("Enter a string: "); fgets(str, sizeof(str), stdin); ptr = str; while (*ptr != '\0' && *ptr != '\n') { length++; ptr++; } printf("Length of string is %d\n", length); return 0; }
#include <stdio.h> int main() { char str[100]; int length; printf("Enter a string: "); fgets(str, sizeof(str), stdin); for (length = 0; length < 100; length++) { if (str[length] == '\0' || str[length] == '\n') break; } printf("Length of string is %d\n", length); return 0; }
Complexity: O(n) time, O(1) space
Time Complexity
The program checks each character once until it finds the end marker, so time grows linearly with string length.
Space Complexity
Only a few variables are used for counting and indexing, so space used is constant.
Which Approach is Fastest?
Using pointers can be slightly faster than indexing, but both are O(n) and efficient for typical string lengths.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Indexing with while loop | O(n) | O(1) | Simple and clear code |
| Pointer increment | O(n) | O(1) | Slightly faster, pointer practice |
| For loop with break | O(n) | O(1) | Clear loop control with fixed size |
'\0' to know where the string ends when not using strlen.'\0' causes incorrect length calculation or reading beyond the string.