All About Strlen() Function in C

All About Strlen() Function in C

4 mins read495 Views Comment
Updated on Nov 23, 2023 14:43 IST

Unveil the strlen() function in C programming, diving deep into its syntax, practical implementations, and code samples. Learn how to effectively utilise strlen() to determine string lengths, manipulate strings, and optimise your C programming proficiency.

2023_03_Copy-of-Feature-Image-Templates-6p1.jpg

In this tutorial, we will discuss the strlen() function in C programming language that is used to determine the length of a string. We will be covering the following sections: 

So, without further ado, let’s get started! 

Explore C Programming Courses

Introduction to strlen() Function in C 

In C programming language, the strlen() function is used to calculate the length of the given string or char array excluding the NULL character (‘\0’), which marks the end of the string. It is a built-in function that is defined in the string.h header file.  

Here is the syntax for the strlen() function: 


 
int strlen(const char *str);
Copy code

Parameters of strlen() 

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

Return Value of strlen() 

Return Type: int 

The strlen() function in C returns an integer value, excluding the null character, which represents the length of the input string. 

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

4 K
2 months
– / –
6 months
– / –
1 month
3.5 K
3 months
15 K
2 months
– / –
50 hours
– / –
40 hours
– / –
2 months
– / –
4 months
5.5 K
140 hours

How to Use strlen() Function in C? 

To utilize the strlen() function in C, you need to: 

  1. Include the header file “string.h” using the preprocessor directive #include <string.h>. 
  2. Declare the string variable that you want to measure. 
  3. Pass the string variable as an argument to the strlen() function. 

The function will return an integer value representing the length of the string (excluding the null character). 

Examples of strlen() Function in C

Example 1: Calculating the length of a string 


 
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Jane Doe";
int length = strlen(str);
printf("The length of the string \"%s\" is %d.\n", str, length);
return 0;
}
Copy code

Output: 

The length of the string "Jane Doe" is 8. 

The above example program defines a character array str and initializes it with the string “Jane Doe”. Then, we call the strlen() function and pass str as a parameter to obtain the length of the string, which is assigned to the integer variable length. 

Finally, the program prints the length of the string and the string itself to the screen using the printf() function. 

Example 2: Counting the number if characters in a string 


 
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, world!";
int count = 0;
int i;
for (i = 0; i < strlen(str); i++) {
if (str[i] != ' ') {
count++;
}
}
printf("The number of characters in the string \"%s\" is %d.\n", str, count);
return 0;
}
Copy code

Output: 

The number of characters in the string "Hello, world!" is 12. 

In the above program, we define a character array str and initialize it with the string “Hello, world!”. We also define two integer variables count and i, where count will be used to count the number of non-space characters in the string and i will be used as a loop counter. 

Next, we utilise a for loop to iterate over each character in the string. The loop starts at index 0 and continues until the index is less than the length of the string, which is determined using the strlen() function. Inside the loop, the program checks if the current character is not a space character. If it is not a space character, then the count variable is incremented. 

After the loop finishes, the program prints the number of non-space characters in the string and the string itself to the screen using the printf() function. 

Example 3: Traversing a string using strlen() and a for loop 


 
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Naukri Learning";
int i;
for (i = 0; i < strlen(str); i++) {
printf("%c\n", str[i]);
}
return 0;
}
return 0;
}
Copy code

Output: 

S
H
I
K
S
H
A

O
N
L
I
N
E

In the above program, we define a character array str and initialize it with the string “Naukri Learning”. We also define an integer variable i, which will be used as a loop counter. 

Next, we utilise a for loop to iterate over each character in the string. The loop starts at index 0 and continues until the index is less than the length of the string, which is determined using the strlen() function. Inside the loop, the program prints the current character to the screen using the printf() function and the %c format specifier. The \n is used to print each character on a new line. 

Example 4: Comparing the length of two strings using strlen() 


 
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Nothing";
char str2[] = "Matters";
if (strlen(str1) > strlen(str2)) {
printf("String \"%s\" is longer than string \"%s\".\n", str1, str2);
} else if (strlen(str1) < strlen(str2)) {
printf("String \"%s\" is shorter than string \"%s\".\n", str1, str2);
} else {
printf("String \"%s\" and string \"%s\" have the same length.\n", str1, str2);
}
return 0;
}
Copy code

Output: 

String "Nothing" and string "Matters" have the same length. 

In the above program, we define a two character arrays str1 and str2 .We initialize them with the strings “Nothing” and “Matters”, respectively.  

Next, we use an if-else statement to compare the lengths of the two strings: 

  • If the length of str1 is greater than the length of str2, the program prints a message indicating that str1 is longer than str2 using the printf() function. 
  • If the length of str1 is less than the length of str2, the program prints a message indicating that str1 is shorter than str2.  
  • If the lengths of the two strings are equal, the program prints a message indicating that the two strings have the same length. 
Header Files in C Programming Language
Malloc in C Programming: How to Allocate Memory Dynamically 
What is Palindrome in C Programming?

Endnotes 

In conclusion, the strlen() function in C is useful for finding a string’s length. This function can be particularly useful when you need to manipulate or work with strings, such as when comparing or concatenating strings. 

Hope this article was helpful to you. If you want to learn more about C programming and solidify your basics, you can explore our articles on C. 

Contributed by Prerna Singh

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio