strlen() function in c
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; } |
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; } |
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; } |
Output :
Length of string is: 5
Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.