C strlen()

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The strlen() function defined in string.h header file calculates the length of the given string or char array excluding the null character in C.

Syntax of C strlen()

The syntax of the strlen() function in C is as follows:

In the above syntax, str refers to the string whose length has to be calculated.

Parameters of C strlen()

The strlen() function in C takes a string as a parameter.

Return Values of C strlen()

Return Type: int

The strlen() function in C returns the length of the string which is passed as the parameter to the function. The length of the string is returned in integer type excluding the NULL character.

Example of C strlen()

In the above code, We created a string named name. We are using the strlen() function to find the length of the string name.

OUTPUT

What is C strlen()?

The string.h header file in C contains many library functions for manipulating an array of characters a.k.a strings.

strlen() is one such library function included under the string.h header file. We know that strings are one-dimensional arrays terminated by a NULL character (\0). The strlen() function in C takes the string as a parameter and returns the length of the string excluding the NULL character. The length includes all the special character, Spaces, Number, Alphabets.

what is strlen in c

The strlen in C comes in handy when we want to calculate the length of a given string. We need not use the traditional method by running a for loop until we encounter a NULL character and increment the count at each iteration.

How to use Strlen in c?

To use strlen() function in C:

  • First, we should include the string.h header file using #include<stdio.h>.
  • The string variable should be declared.
  • The string variable should be passed as a parameter to the strlen() function.
  • The length of the string is returned by the function.

More Examples of C strlen()

Example 1: Calculating the length of a string without any spaces.

Output:

Explanation:

In the above code, we used the string.h header so that we can make use of the strlen() library function. In the main() function, we have declared a string named str and initialized it with a string Hello. Later, we used the strlen() function to find the length of the string.

Example 2: Calculating the length of an alphanumeric string.

Output:

Explanation: In the above code, passed an alphanumeric string as a parameter to the strlen() function. In the output, we can observe that both numbers and alphabets are considered while calculating the length of the string.

Example 3: Calculating the length of string with special characters.

Output:

Explanation: In the above code, passed a string that contains special characters as a parameter to the strlen() function. In the output, we can observe that even the special character is considered while calculating the length of the string.

Example 4: Calculating the length of string with spaces in between characters.

Output:

Explanation: In the above code, passed a string that contains spaces as well as a parameter to the strlen() function. In the output, we can observe that even the space is considered while calculating the length of the string.

Example 5:

Output:

Conclusion

  • The strlen() function in C is used to find the length of a string.
  • The strlen() function in C returns an integer with the length of the string excluding the NULL character.
  • The strlen() function counts the alphabets, whitespaces, special symbols, and numbers until it encounters the NULL character in the string.

See Also