The strlen() function calculates the length of a given string.The strlen() function is defined in string.h header file. It doesn’t count null character ‘\0’.
Syntax:
int strlen(const char *str);
Parameter:
- str: It represents the string variable whose length we have to find.
Return: This function returns the length of string passed.
Below programs illustrate the strlen() function in C:
Example 1:-
// c program to demonstrate // example of strlen() function. #include<stdio.h> #include <string.h> int main() { char ch[]={'g', 'e', 'e', 'k', 's', '\0'}; printf("Length of string is: %d", strlen(ch)); return 0; } |
chevron_right
filter_none
Output :
Length of string is: 5
Example 2:-
// c program to demonstrate // example of strlen() function. #include<stdio.h> #include <string.h> int main() { char str[]= "geeks"; printf("Length of string is: %d", strlen(str)); return 0; } |
chevron_right
filter_none
Output:
Length of string is: 5
Example 3:-
// c program to demonstrate // example of strlen() function. #include<stdio.h> #include <string.h> int main() { char *str = "geeks"; printf("Length of string is: %d", strlen(str)); return 0; } |
chevron_right
filter_none
Output :
Length of string is: 5
Recommended Posts:
- Difference between strlen() and sizeof() for string in C
- Write your own strlen() for a long string padded with '\0's
- arc function in C
- strtod() function in C/C++
- iswdigit() function in C/C++
- vwprintf() function in C/C++
- vswscanf() Function in C/C++
- iswupper() function in C/C++
- putwchar() function in C/C++
- iswlower() function in C/C++
- Address of a function in C or C++
- towctrans() function in C/C++
- iswcntrl() function in C/C++
- strlwr() function in C
- fegetexceptflag() function in C/C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



