C Program to Find the Length of a String

Finding the length of a string is a fundamental operation in many programming tasks. In C, this can be done in various ways. This article presents three different C Program to Find the Length of a String with explanations, example code, and outputs.

Prerequisites

Before diving into the solutions, ensure you have the following prerequisites:

  • Basic understanding of C programming.
  • Familiarity with strings and loops.
  • A C compiler installed on your system (e.g., GCC).

Solution 1: Using a Loop

1.1 Explanation

In this approach, we manually iterate through the string using a loop, counting the number of characters until we reach the null terminator '\0'.

1.2 Program

C
#include <stdio.h>

int stringLength(char *str) {
    int length = 0;
    while (str[length] != '\0') {
        length++;
    }
    return length;
}

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Remove the newline character introduced by fgets
    str[stringLength(str) - 1] = '\0';

    printf("Length of the string: %d\n", stringLength(str));
    return 0;
}

1.3 Output

C
Enter a string: Hello, World!
Length of the string: 13

Solution 2: Using the Standard Library Function using strlen

2.1 Explanation

The C standard library provides a built-in function strlen to compute the length of a string. This function is defined in the string.h header file.

2.2 Program

C
#include <stdio.h>
#include <string.h>

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Remove the newline character introduced by fgets
    str[strlen(str) - 1] = '\0';

    printf("Length of the string: %lu\n", strlen(str));
    return 0;
}

Output

C
Enter a string: Programming is fun!
Length of the string: 18

Solution 3: Using Recursion

3.1 Explanation

In this approach, we use recursion to determine the length of the string. The base case is when the current character is the null terminator '\0', at which point we return 0. Otherwise, we return 1 plus the length of the rest of the string.

3.2 Program

C
#include <stdio.h>

int stringLength(char *str) {
    if (*str == '\0') {
        return 0;
    } else {
        return 1 + stringLength(str + 1);
    }
}

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    // Remove the newline character introduced by fgets
    str[stringLength(str) - 1] = '\0';

    printf("Length of the string: %d\n", stringLength(str));
    return 0;
}

Output

C
Enter a string: Recursion is powerful.
Length of the string: 21

Conclusion

Using C Program to Find the Length of a String is a fundamental operation in programming. This article presented three different methods to achieve this task: using a loop, using the standard library function strlen, and using recursion. Each method has its own advantages and can be chosen based on the specific requirements of your application. By understanding and experimenting with these techniques, you can efficiently work with strings and enhance your overall programming skills in C.