To determine the length of a string in C, use the strnlen function. The method that returns the string’s character count is called the string length. Any string that uses this approach has the length of the characters in the string. The strnlen() function determines the size of the string referred to by the character “s”, excluding the final null character and up to the maxlen bytes. After the first maxlen bytes, the function stops checking. This function is slightly different from the strlen function. The characters in a string “s” that come before its last null character are indicated by the strlen() function. The strnlen() function produces either the same outcome as the strlen() or maxlen, whichever is fewer. We can determine a fixed-size string’s length with the strnlen command. Here, we will look at some instances that returns a string length by using the strnlen function in the C language while using the Ubuntu 20.04 terminal.
Syntax:
The strnlen function takes only two parameters which include the pointer variable “s” of the char data type and the “size_t” maxlen. Here, the parameter “s” points to a string that has a null end. The “size_t” maxlen represents the most characters that should be scanned before returning a string end marker. The strnlen() method returns the maximum of the maxlen bytes in the string referenced to by “s”, ignoring the ending null byte (‘0’).
The strnlen() function never examines any characters past the s[maxlen-1] when doing this. Instead, it just scans the first maxlen characters in the string linked to by “s”. Now, we use this strnlen syntax in our code implementation with the C language.
Example 1: Program of Strnlen Function
The string.h library contains the preset string functions for C, including the strnlen function. One variable type, one macro, and many functions for working with the character arrays are all defined in the header file “string.h”. Several functions for working with strings are declared in the C header file “string.h”. The right way of using the strnlen function is demonstrated in the following:
Every program’s first step is to include all the required libraries for the code to execute perfectly. So, we added the three main libraries for this program. Inside that main method, void is passed as a parameter. In this situation, the main can accept a maximum of zero arguments. Consequently, since the main function doesn’t accept any parameters, it essentially utilizes the int main (void).
Then, we called the three printf commands. For these commands, the ‘%ld’ format is passed as a statement. To represent the integer numbers, the %d format specifier is used. The integer value kept in the variable is printed, utilizing the printf() function. The strnlen is also declared in the printf method. We defined two parameters: one is the string value and the other is the integer value. Note that the first and second strnlen functions are without the null character and the last one has the null character.
Now, with the gcc compiler, we have given a file name of the code where the strnlen function is used. As the code file is inside the strnlen.c, we have given a command like this: gcc strnlen.c -o strnlen. The code is compiled in the following image. By giving the “./strnlen” file name, the subsequent output is produced. Since the first two strnlen functions contain some string values, the strnlen of the string is displayed. The last line of the output that returns the value 0 as the defined string has null characters.
Example 2: Program of Strnlen Function to Get Maxlen
Let’s see what exactly this title example means by implementing the example code:
Here, we defined the C libraries to perform the strnlen functionality and other commands. Then, we have the int main function. An int serves as a particular kind of the return main. In other term, the function is now expected to receive a value of the result type int (integers). A return statement is also included.
After a program is finished, it should preferably return its value of 0 to the primary one. Inside the main block, we created a str1 variable. An array of chars is produced by the char str1[100] expression. We can store 100 characters in this character array, ranging from index 0 to index 99. Then, initialize it with the string containing the different words. The printf command here is used to print the length of the string characters as we passed the strnlen function to this command. The first strnlen assigned the maxlen 30 and the second one assigned the maxlen 10.
The strnlen function on compilation generates the following maxlen of the string characters:
Conclusion
This post seeks to inform you about the C language’s strnlen function. Here, we showed how to apply the strnlen function. We explained the syntax of the strnlen function with each parameter it takes and the return value from this particular function. The strnlen() operation returns the maxlen of the character strings if there are no null ending (‘0’) characters among the first characters of the maxlen given to by the “s” variable. Otherwise, it returns strnlen(s) if that is less than the maxlen. We gave two examples for a better understanding of the strnlen function in the C scripting language that clears your vision about this strnlen function of the C language.