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

</>
Copy
#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:

  1. We declare a character array str[] and initialize it with “Hello, World!”.
  2. We initialize an integer variable length to 0, which will store the count of characters.
  3. We use a for loop to iterate through the string.
  4. The loop runs until it encounters the null character '\0', which marks the end of the string.
  5. In each iteration, length is incremented.
  6. 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

</>
Copy
#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:

  1. We declare a character array str[] initialized with “C Programming”.
  2. We initialize two integer variables: length (set to 0) to store the count and i (set to 0) to traverse the string.
  3. The while loop runs as long as str[i] != '\0'.
  4. In each iteration, length is incremented, and i moves to the next character.
  5. 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

</>
Copy
#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:

  1. We define a recursive function findLength() that takes the string and an index as arguments.
  2. The base case checks if str[index] == '\0'. If true, the function returns 0.
  3. Otherwise, it returns 1 plus the result of the function called with the next index.
  4. In main(), we call findLength() with the string and index 0.
  5. 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():

  1. for Loop: Iterates through the string and counts characters.
  2. while Loop: Uses a condition to count until '\0' is reached.
  3. 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.