C Program to Find the Length of a String

In this article, we will write a C program to find the length of a string.

The program takes a string from the user as input, calculates its length and prints it back on the output screen.

Sample Input:

Enter a string: Hello, world!

Sample Output:

The length of string is: 13

In C, strings are an array of characters that end with a null character '\0'. The null character represents the end of the string.

Therefore, if you want to get the length of a string, you can loop through the string until you reach the null character and increment the counter in each iteration of the loop to calculate the string length.

The following C program shows how you can calculate the length of a string:

// C program to find the length of a string
#include <stdio.h>
#include <string.h>

int main() {
    
    char str[100];
    int length = 0;
    
    // Take string from the user
    printf("Enter a string: ");
    gets(str);
    
    // Loop until end of the string is reached
    while(str[length]!='\0'){
        // Increment length in each iteration
        length++;
    }
    
    printf("The length of string is: %d", length);

    return 0;

}

Output:

Enter a string: Hello, world!
The length of string is: 13

Code Explanation:

  1. The program first declares a character array str of size 100 and an integer variable length to store the length of the string.
  2. The user is then prompted to enter a string using the printf() function, and the gets() function is used to read the input string from the user.
  3. The program then enters a while loop that continues until the end of the string is reached.
  4. Inside the loop, the length variable is incremented in each iteration to count the number of characters in the string.
  5. Finally, the program prints the length of the string using the printf() function.

2. Find the Length of the String using strlen() Function

You can also use the strlen() function to get the length of a string. The strlen() function is a built-in function which is defined under the string.h header library.

The strlen() function takes a string parameter and returns an integer value which represents the length of the string.

int strlen(const char *str);

The following C program shows how you can calculate the length of a string using strlen() function:

// C program to find the length of a string
// using strlen() function
#include <stdio.h>
#include <string.h>

int main() {
    
    char str[100];
    int length;
    
    // Take string from the user
    printf("Enter a string: ");
    gets(str);
    
    // Get the length of the string
    length = strlen(str);
    
    printf("The length of string is: %d", length);

    return 0;

}

Output:

Enter a string: Hii there!
The length of string is: 10

You can also loop through the string using other loops such as a for or do-while loop to iterate through the string and gets its total length.

However, the basic concept remains the same in each approach.

Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.