The strlen function computes the length of the given string. It takes a string as an argument and returns its length. It doesn’t count the null character ( ‘\0’ ).
The strlen() function defined in the <string.h> header file, so you have to include it before using it.
Syntax strlen in C:
//Syntax of strlen size_t strlen(const char *s);
Parameters:
s— pointer to the null-terminated string to be scanned.
Return:
The strlen function returns the number of characters that precede the terminating null character.
Let’s see an example code to understand the functionality of the strlen in C. In this C code, I am calculating the length of the string using the strlen function.
#include <stdio.h> #include <string.h> int main() { char str[20]="Aticleworld"; // Using %zu format specifier to print size_t printf("Length of string str = %zu \n",strlen(str)); return 0; }
Output:
Length of string str = 11
Explanation:
The strlen function counts the number of characters from the beginning of the string to the terminating null character. Because the string “Aticleworld” contains only 11 characters just before the null character, that is why strlen is returning 11.
Important points you must know before using strlen in C:
1. You must include string.h header file before using the strlen() function in C.
2. strlen() count the characters until not get the null character ( ‘\0’ ). Let’s see an example code,
#include <stdio.h> #include <string.h> int main() { char str[20]="Atic\0leworld"; // Using %zu format specifier to print size_t printf("Length of string a = %zu \n",strlen(str)); return 0; }
Output:
In the input string, a null character is present just after the 4th char, so it is returning 4.
Difference between strlen() and sizeof() for string in C:
Here I am mentioning some important differences between strlen() and sizeof():
strlen() | sizeof() |
The strlen() is a predefined function defined in “string.h” header file. | sizeof() is a unary operator or compile-time expression giving you the size of a type or a variable’s type. |
strlen() is used to get the length of a string stored in an array. | sizeof() is used to get the actual size of any type of data in bytes. |
It counts the numbers of characters in a string eliminating null values. | It returns the size of an operand, not the string length (including null values). |
It gives you the length of a C-style null-terminated string | Sizeof() does not care about the variable value. |
Syntax of strlen is size_t strlen(const char *s); | Syntaxes of sizeof are: sizeof(type), sizeof expression |
Note 1: The strlen and sizeof are quite different. In C++ strlen() does not use too much because it is for C-style strings, which should be replaced by C++-style std::strings.
Note 2: The primary application for sizeof() in C is as an argument to functions like malloc(), memcpy() or memset(), all of which you shouldn’t use in C++ (use new, std::copy(), and std::fill() or constructors).
How to implement your own strlen function?
Your compiler/standard library will likely have a very efficient and tailored implementation of the strlen() function. So if not required avoid creating your own version of the strlen function.
We can implement the strlen function in many ways. Here we are implementing strlen using the help of the while loop. In the loop, we will count the number of characters until not get the null character. So let’s create our own version of the strlen() function in C.
Note: Below function only to understand the working of strlen. There are a lot of scenarios that are not handled in this function. You can handle the scenario as your requirement and if possible then use the standard strlen function (library function).
unsigned int my_strlen(const char *s) { unsigned int count = 0; while(*s!='\0') { count++; s++; } return count; }
Let’s create a small application to test our own created strlen() function in C.
#include <stdio.h> unsigned int my_strlen(const char *s) { unsigned int count = 0; while(*s!='\0') { count++; s++; } return count; } int main() { char str[20]="Aticleworld"; printf("Length of string a = %u \n",my_strlen(str)); return 0; }
Output:
Length of string str = 11
You can also implement your own version of the strlen function in a single line using the help of for loop.
//function behave like strlen void my_strlen(const char *s, size_t *len) { for (*len = 0; s[*len]; (*len)++); }
Recommended Articles for you:
- Best Gifts for the programmer and techies.
- Implement and use of memset of in C
- Use and create strcspn in programming.
- Use of memchr in C programming.
- strrchr in C programming.
- strchr in C programming.
- How to Use strncpy() and implement own strncpy().
- How to pass an array as a parameter?
- Implement own atoi in C.
- 10 Best C Programming Books.
- Best mouse for a programmer.
- Dangling, Void, Null, and Wild Pointers
- Memory Layout in C.
- File handling in C, In a few hours.