0
0
CProgramBeginner · 2 min read

C Program to Find String Length Without strlen Function

You can find the length of a string in C without strlen by looping through each character until you reach the null character '\0', counting each step with a variable like length.
📋

Examples

InputHello
OutputLength of string is 5
InputC programming
OutputLength of string is 13
Input
OutputLength of string is 0
🧠

How to Think About It

To find the length of a string without using 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

1
Start with a count variable set to zero.
2
Look at the first character of the string.
3
If the character is not the end marker <code>'\0'</code>, increase the count by one and move to the next character.
4
Repeat step 3 until you find the end marker <code>'\0'</code>.
5
Return or print the count as the length of the string.
💻

Code

c
#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;
}
Output
Enter a string: Hello Length of string is 5
🔍

Dry Run

Let's trace the input "Hello" through the code to find its length.

1

Initialize length

length = 0

2

Check first character

str[0] = 'H' (not '\0' or '\n'), length = 1

3

Check second character

str[1] = 'e' (not '\0' or '\n'), length = 2

4

Check third character

str[2] = 'l' (not '\0' or '\n'), length = 3

5

Check fourth character

str[3] = 'l' (not '\0' or '\n'), length = 4

6

Check fifth character

str[4] = 'o' (not '\0' or '\n'), length = 5

7

Check sixth character

str[5] = '\n' (stop counting)

IndexCharacterLength
0H1
1e2
2l3
3l4
4o5
5\nstop
💡

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

Pointer increment
c
#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;
}
This uses a pointer to move through the string instead of indexing, which can be more efficient and shows a different way to access characters.
For loop with break
c
#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;
}
This uses a for loop with a break condition, which some find clearer for fixed-size arrays.

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.

ApproachTimeSpaceBest For
Indexing with while loopO(n)O(1)Simple and clear code
Pointer incrementO(n)O(1)Slightly faster, pointer practice
For loop with breakO(n)O(1)Clear loop control with fixed size
💡
Always check for the null character '\0' to know where the string ends when not using strlen.
⚠️
Forgetting to stop counting at the null character '\0' causes incorrect length calculation or reading beyond the string.