Length of a String (without strlen): flowchart, algorithm and program
This program reads a word and counts its characters with a while loop, so no built-in length function is needed. A C string always ends with the null character '\0', so we count characters until we reach it.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Set count = 0.
- Step 3: Read word.
- Step 4: If word[count] != '\0', go to step 5; otherwise go to step 6.
- Step 5: Increase count by 1. Go to step 4.
- Step 6: Print count.
- Step 7: Stop.
Pseudocode
BEGIN
count ← 0
READ word
WHILE word[count] != '\0' DO
count++
END WHILE
PRINT count
END
Program in C, Python and Java
Length of a String (without strlen) program in C
#include <stdio.h>
int main() {
char word[100];
int count = 0;
printf("Enter a word: ");
scanf("%s", word);
while (word[count] != '\0') {
count++;
}
printf("Length = %d\n", count);
return 0;
}Length of a String (without strlen) program in Python
word = input("Enter a word: ")
count = 0
for ch in word:
count += 1
print("Length =", count)Length of a String (without strlen) program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = sc.next();
int count = 0;
for (char ch : word.toCharArray()) {
count++;
}
System.out.println("Length = " + count);
}
}
Sample input: word = hello
Output: Length = 5
How it works
- scanf("%s", word) reads the word and stores it in the char array word. C automatically adds '\0' after the last character.
- count starts at 0. It does two jobs: it is the index of the character being checked, and it is the running total.
- The loop condition word[count] != '\0' checks the current character. While it is not the end marker, count++ moves on to the next one.
- When word[count] is '\0', the loop stops. At that point count equals the number of characters before it, which is the length.
- For "hello": word[0] to word[4] are h, e, l, l, o, so count goes from 0 to 5. word[5] is '\0', the loop ends, and Length = 5 is printed.
Dry run (trace table)
| Step | Line | Box | count | word | Output |
|---|---|---|---|---|---|
| 1 | 3 | Start | |||
| 2 | 5 | count = 0 | 0 | ||
| 3 | 8 | Read word | 0 | hello | Enter a word: |
| 4 | 10 | word[count] != '\0'? | 0 | hello | |
| 5 | 11 | count++ | 1 | hello | |
| 6 | 10 | word[count] != '\0'? | 1 | hello | |
| 7 | 11 | count++ | 2 | hello | |
| 8 | 10 | word[count] != '\0'? | 2 | hello | |
| 9 | 11 | count++ | 3 | hello | |
| 10 | 10 | word[count] != '\0'? | 3 | hello | |
| 11 | 11 | count++ | 4 | hello | |
| 12 | 10 | word[count] != '\0'? | 4 | hello | |
| 13 | 11 | count++ | 5 | hello | |
| 14 | 10 | word[count] != '\0'? | 5 | hello | |
| 15 | 14 | Print count | 5 | hello | Length = 5 |
| 16 | 16 | Stop | 5 | hello |
Time and space complexity
Time: O(n) · Space: O(1). The loop visits each of the n characters once and uses only a counter as extra memory.
Common mistakes
- Counting the '\0' as a character, which gives a length one too large. The loop must stop when it sees '\0', not after it.
- Writing '0' instead of '\0'. '0' is the digit character, while '\0' is the null terminator.
- Expecting scanf("%s") to read a sentence. It stops at the first space, so "hello world" would give a length of 5. Use fgets for text with spaces.
- Using an array that is too small, such as char word[5] for "hello". There is no room for '\0', so the loop can run past the array.
Viva questions
Q1. Why is count initialised to 0 in line 5?
count starts at 0 because it is both the number of characters counted so far and the index of the next character to check. For an empty string, the loop never runs and 0 is printed.
Q2. What does the condition word[count] != '\0' on line 10 check?
It checks whether the current character is the null terminator that marks the end of a C string. While it is not '\0', there is another real character to count.
Q3. Why did you use a while loop and not a for loop?
We don't know the length in advance; we stop when we reach '\0', so a while loop fits. A for loop would also work, but the while form shows the 'repeat until the end marker' idea more clearly.
Q4. Who puts the '\0' at the end of the word, and does it get counted?
scanf with %s stores the characters you typed and automatically adds '\0' after them. It is not counted, because the loop stops as soon as it sees it.
Q5. What happens if the user types 'hello world' with a space? Why?
Only 'hello' is stored and the length printed is 5. scanf with %s stops reading at whitespace, so 'world' is left in the input buffer.
Q6. What is a problem with scanf("%s", word) when the array size is 100?
If the user types 100 or more characters, scanf writes past the end of the array (buffer overflow), since it has no size limit. Using scanf("%99s", word) prevents this, and the array can hold at most 99 characters plus '\0'.
Q7. What is the time complexity of this program, and what is the extra space used?
Time is O(n), where n is the length of the string, because each character is visited exactly once. Extra space is O(1), just the count variable (apart from the input array).
Q8. What would you change to read a full sentence with spaces and still count its length?
Replace scanf with fgets(word, 100, stdin) or scanf("%[^\n]", word) to read the whole line. With fgets the newline is also stored, so you would subtract 1 or stop the count at '\n' too.
Frequently asked questions
How do you find the length of a string in C without strlen?
Start a counter at 0 and loop while word[count] is not '\0', increasing the counter each time. When the loop ends, the counter holds the length.
Why do we stop at '\0'?
C strings are character arrays that end with the null character '\0'. It marks where the string ends, so reaching it means every real character has been counted.
Does this program count spaces?
The counting loop does count spaces, but scanf("%s") stops reading at the first space. To count a full sentence, read it with fgets and handle the trailing newline.