C code to find length of a string

strLen:c code for user defined function to find length of a string
#include<stdio.h>
int main()
{
char arr[20];
printf("Enter a string:\n");
gets(arr);

int result;
result=strLen(arr);
printf("Length of the string is:%d\n",result);
return 0;
}

int strLen(char *str)
{
char *tmp;
tmp=str;
while(*tmp)
{
tmp++;
}
return (tmp-str);
}
/*
-bash-3.2$ ./a.out
Enter a string:
India
Length of the string is:5
-bash-3.2$ ./a.out
Enter a string:
India is great
Length of the string is:14
*/


No comments:

Post a Comment