C strlen() function
string function in c
There are several library functions used to manipulate strings. These all functions are defined in the string.h header file.
In this section we learn some of the commonly used string functions like strlen(),strcpy(),strcat(),strcat() etc
Declaration:
size_t strlen(char const *string);
|
This strlen() return the length of the string, such as number of characters of the string or in the string excluding the null character. its accepts one or single argument. which is pointer to the first character of the string. take the example to understand strlen(“Rajesh”) returns the intger value 6. similar such as str is the character array that contains “Saraswat” then the strlen(str) returns the integer value 8 that is the size of character array.
let us take example with program
/* use strlen()*/
main()
{
char arr[30];
printf("Enter the string to find size");
gets(arr);
printf("The length of the string = %u\n",strlen(arr));
0;
}
|
output:
Enter the string to find size Rajkumar
The length of the string = 8
|
let us see how we can create like this function
unsigned int astrlen(char arr[])
{
i=0;
(arr[i]!=null)
{
i++
}
i;
}
|
// example of strlen() function.
int main()
{
char *str = "programmingknow";
printf("Length of string is: %d", strlen(str));
return 0;
}
|