How to use strlen() in C?
The strlen() function in C is a commonly used function in string manipulation. It is a standard library function that is part of the string.h header file. The purpose of strlen() is to determine the length of a given string, which is defined as the number of characters in the string excluding the null terminator character '\0'. In this article, we will explore how to use the strlen() function in C, including a description of its syntax, arguments, and some examples to demonstrate its usage.
Syntax:
The syntax of the strlen function is as follows:
int strlen(const char *str);
Here, str is a pointer to the string whose length we want to determine, and the function returns an unsigned int value that represents the length of the string. The function takes a single argument, which is a pointer to the string, and returns the length of the string as an integer.
Arguments:
The strlen() function takes a single argument, which is a pointer to the string. This argument must be a null-terminated string, meaning that the last character in the string must be the null terminator '\0'. If the argument is not a null-terminated string, the behaviour of the strlen function is undefined.
Return Value:
The strlen function returns the length of the string as an unsigned int value. This value represents the number of characters in the string, excluding the null terminator character '\0'.
Now, we have a basic understanding of the strlen function; let's look at some examples to see how it works.
Example 1: Calculating the Length of a String
#include <stdio.h>
#include <string.h>
int main(void) {
char str[50] = "Hello, World!";
int length;
length = strlen(str);
printf("The length of the string is: %d\n", length);
return 0;
}
Output:
The length of the string is: 13
In this example, we have declared a string str with the value "Hello, World!". Then, we have called the strlen function and pass the str string as an argument. The function returns the length of the string, which is 13, and we print it to the screen.
Example 2: Finding the Length of a User-Input String
#include <stdio.h>
#include <string.h>
int main(void) {
char str[100];
int length;
printf("Enter a string: ");
gets(str);
length = strlen(str);
printf("The length of the string is: %d\n", length);
return 0;
}
Output:
Enter a string: Javatpoint
The length of the string is: 10
In this example, we declare a string str with a maximum length of 100 characters. We then use the gets function to read a string from the user and store it in the str string. Finally, we call the strlen function and pass the str string as an argument, and print the length of the string to the screen.
Example 3: Finding the Length of a Multiline String
#include <stdio.h>
#include <string.h>
int main(void) {
char str[100] = "Hello,\nWorld!\n";
int length;
length = strlen(str);
printf("The length of the string is: %d\n", length);
return 0;
}
Output:
The length of the string is: 14
In this example, we have declared a multiline string ‘str’ with the value "Hello,\nWorld!\n". The strlen() function considers each newline character '\n' as a separate character, so the length of the string is calculated accordingly. In this case, the length of the string is 14.
Additionally, it is important to note that the strlen() function is a relatively simple function to implement, but it is still recommended to use the standard library version as it is more optimized and tested. The implementation of strlen() typically involves iterating through the string until a null terminator character '\0' is encountered, and counting the number of characters encountered along the way.
The strlen() function is commonly used in various string manipulation and processing tasks, such as searching for substrings, concatenating strings, or trimming whitespace from a string. For example, the strlen() function can be used to determine the length of a string that a user inputs, and then allocate enough memory to store the string. In this case, the strlen() function can help prevent buffer overflow attacks by ensuring that the memory allocation is proportional to the length of the string.
Another common use of strlen() function is to determine the length of strings in an array of strings. For example, consider an array of strings where each string represents a word. The length of each word can be determined using the strlen() function, which can then be used to sort the words in the array based on their length.
Conclusion:
The strlen()function in C is a useful function for determining the length of a given string. It is a standard library function that is part of the ‘string.h’ header file and is used to calculate the number of characters in a string, excluding the null terminator character '\0'. When using the strlen() function, it is important to ensure that the string passed as an argument is a null-terminated string, as the behaviour of the function is undefined for non-null-terminated strings. The function takes a single argument, which is a pointer to the string, and returns the length of the string as an ‘unsigned int’ value. By following the syntax and examples provided in this article, you should be able to use the strlen() function effectively in your own programs.