1.
Introduction
2.
Problem
3.
Solution
3.1.
Code
3.2.
Input
3.3.
Output
3.4.
Explanation
4.
4.1.
What is C?
4.2.
What is a string?
4.3.
How do you determine the length of a string?
4.4.
Is strlen a library function in C?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

# C Program To Count Length Of String Using Library Function

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

## 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().

## Problem

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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## 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)

Also checkout Starcat()

### 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

Must Read what is storage class in c and Decision Making in C

#### 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 Coding Ninjas Studio 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!!!

Live masterclass