C strlen

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()*/
#include<stdio.h>
#include<string.h>
int
main()
{
char arr[30];
printf("Enter the string to find size");
gets(arr);
printf("The length of the string = %u\n",strlen(arr));
return 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[])
{
int i=0;
while(arr[i]!=null)
{
i++
}
return i;
}

// example of strlen() function.
#include<stdio.h>
#include<string.h>
int main()
{
char *str = "programmingknow";
printf("Length of string is: %d", strlen(str));
return 0;
}

Length of string is: 15