strlen in C: A Comprehensive Guide
Learn how to use the built-in strlen function, as well as how to build your custom strlen function from scratch.
Before I go into the strlen string function properly, I would like you to understand that there are various built-in functions defined in the string.h header file, these functions are used to manipulate strings in the C programming language. They are used to perform various operations such as finding the length of a string, comparing two strings and even concatenating(combining) two strings. etc.
I will be focusing on the strlen string function in this article.
Introduction to strlen
strlen, known as the string length is the built-in function defined in the string.h header file that is used to find the length of a string. The function can be used on a word or groups of words, so far it is a string in the C Programming Language, that is anything enclosed in the double quote (" "), apart from the null character ('\0') will be counted and the number returned will be the number of all the characters (letters, numbers, symbols and space etc.) contained in the string.
The Built-in strlen Function
Just as we have described the string functions in the introductory part of this article, so also the strlen like others is a built-in function that exists in the header file called string.h, so to use this built-in function because it has already been defined, we can simply just call it and pass in the string we want to count as the argument, which it will return an integer, that is a number that shows the number of characters contained in that string.
To use the built-in strlen function, you just simply need to first define at the beginning of your program the string.h header file, afterward, inside your main function or whatsoever function you want to use, you simply call the string function name (which in this case is strlen) and pass the string whose length you want to find.
Here is an example to show you how to do that:
#include <stdio.h>
#include <string.h>
int main(void)
{
//stored the string "Good Day Fellow Developer" in the greeting variable
char *greetings = "Good Day Fellow Developer";
//declare the count variable to store the length there
int count;
//used the strlen to count the length of string stored in the greeting variable
count = strlen(greetings);
//prints the length of the string
printf("%d\n", count);
//return 0 since the main function is declared to return a string
return (0);
}
Now if you count the string stored in the greeting variable, which is "Good Day Fellow Developer"
includes the space between each word, and you will see that it is 25.
Building a Custom strlen Function
Just like how we have the strlen as a built-in function, we can also have it as a normal function declared and defined by the user itself as against using the standard inbuilt string function.
Before we talk about creating our own custom strlen string function in C, let us talk about the behavior of the inbuilt strlen string function and how it works.
To find out more about how the default strlen works, you can search man strlen
to see how it works.
To write a custom strlen function, one very vital thing is that all string ends with a null character, the null character is \0
so with this knowledge, we now apply our loop, which could be the for-loop, while-loop or do-while loop to iterate through each of the characters in the string, including the space too, as space in C is also regarded as a character. Let us look at the algorithm for solving this;
Given a string of characters, let's say:
"Software Engineering is Great"
.Declare
i
andcount
as anint
, to store the index number and string count respectively.Using any of the loops (for-loop, while-loop or do-while-loop), in this case, I will be concentrating on the for-loop.
With the index count starting from zero (0), start looping, and keep looping until you get to the null character
\0
as the null character is available at the end of every string, thus, marking the end of the string.Once the loop gets to the point where it meets the null character, which is practically the end of the string, then it should end/exit the loop.
It should also return/print the number of counts from the loop that is just exited or ended.
Now let us look at it in the form of a diagram, before we implement the algorithm to create our strlen, which we will name _strlen.
So starting our index with 0, the loop will check if the character at the index zero(0) of the string is equal to the null character, if it is not, then it will increment the count by 1 and then move to the next character and check again. It will keep going like that until it gets to when the character in the index is a null character, then it will end the loop. And in the body of the loop, we will keep increasing the number of counts for each loop, until the loop stops, only then will we return/print the count to see the length of the string.
For this explanation, it means that we will initialize our loop with 0 and then give it a condition to continue looping and checking character by character until it gets to the null character '\0'.
The empty boxes signify the space between each of the words.
Thus, our custom strlen becomes:
int _strlen(char *str)
{
int i, count = 0;
for (i = 0; str[i] != '\0'; i++)
{
count++;
}
return (count);
}
To test it with the string we mentioned above, we can introduce the main function in our c;
#include <stdio.h>
int _strlen(char *str);
int main(void)
{
char *str;
int len;
str = "Software Engineering is Great";
len = _strlen(str);
printf("%d\n", len);
return (0);
}
int _strlen(char *str)
{
int i, count = 0;
for (i = 0; str[i] != '\0'; i++)
{
count++;
}
return (count);
}
Running this code will give 29, which is the full length of the string. Here is what you will get:
We will get the result below, after compiling and running the code:
Conclusion
The string inbuilt functions in string.h can also be built from scratch by anybody, given the right logic and concept behind the function, and an understanding of how the function works and what it does.
Thank you for reading. Let us connect on Twitter and LinkedIn.