Update appNew update is available. Click here to update.

C Program To Count Length Of String Using Library Function

Aman Kumar Singh
Last Updated: Mar 15, 2023
EASY

Introduction

In this blog, we will learn the C program to count length of string using library function. There are many ways to find the length of the string in C, but the easiest way is to use the library function. We will see the code to find the length of the string using the library function, along with an explanation.

Length of a string: The number of characters between the beginning of the string and the terminating null character determines the length of the string ("\0").

For example “ninja” string is stored in the memory as given below.

You can observe that there is a null character(\0) after the characters in the string, which indicates that the string is terminated.

The library function that we use to find the length of a string in C is strlen().

You can also read about dynamic array in c.

Problem

Given a string as input, find the length of the string.

Solution

strlen() is a pre-defined function in C that returns the length of a string which it takes as an argument. We need to include the string.h header file to use the strlen() function.

Code

#include<stdio.h>
#include<string.h>
 
int main() {
    char str[100];
    int len;
    
    printf("\nEnter the String : ");
    scanf("%s",str);
    
    /*
        strlen() is the pre-defined function
        to find the length of a string
    */
    len = strlen(str);
    
    printf("\nLength of the given string is %d", len);
    return(0);
}

Input

Ninja

Output

5


You can practice by yourself with the help of online c compiler.

Time complexity: O(n)

Space complexity: O(1)

Explanation

We take an input string and store it in the str variable. Now we give str as an argument to the strlen() function. This function returns the length of the str string. We output the length of the string, or we can use it as required in the program.

The length of the string should not be confused with the size of the array which holds the string. 

For example: char str[100] = “Ninja”;

sizeof(str) = 100

strlen(str) = 5

Here the size of the array is 100, but it has been initialized with a string of length 5 only. So the strlen() function will output 5 as the length of the string and not 100.

Finding the length of a string is one of the basic operations which will prove very helpful in your future coding journey.

Check out this problem - Multiply Strings

Frequently Asked Questions

What is C?

The C programming language is a general-purpose programming language. Dennis Ritchie designed the C programming language in the 1970s, and it is still extensively used and important today.

What is a string?

A string is defined as a collection of characters. A character array differs from a string in that the string is terminated with the special character '0'.

How do you determine the length of a string?

The length of a string is determined by the number of characters between the beginning of the string and the terminating null character("\0").

Is strlen a library function in C?

Yes, strlen is a library function in C. We need to include the string.h header file to use this library function in our program.

Conclusion

This article extensively discussed the C program to count length of string using library function. We hope that this blog has helped you enhance your knowledge of the C programming language and if you would like to learn more, check out our articles on

Refer to our carefully curated articles and videos and code studio library if you want to learn more. Refer to our guided paths on Codestudio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning!!!

Was this article helpful ?
0 upvotes