TopBottom

Followers



Click on more
SUBSCRIBE

Enter your email address:

Delivered by FeedBurner



VIDEO

Announcement: wanna exchange links? contact me at ravikrak@yahoo.com

strlen() function

Posted by Ravi Kumar at Friday, September 23, 2011
Share this post:
Ma.gnolia DiggIt! Del.icio.us Yahoo Furl Technorati Reddit

This function counts the number of characters present
in a string. Its usage is illustrated in the following
program.

main()
{
char arr[] = "Bamboozled";
int len1, len2;
len1 = strlen (arr);
len2 = strlen ("Humpty Dumpty");
printf("\nstring = %s length = %d",arr,len1);
printf("\nstring = %s length = %d", "Humpty Dumpty", len2);
}

output:
string = Bamboozled length = 10
string = Humpty Dumpty length = 13

Note that in the first call to the function strlen(),
we are passing the base address of the string, and the
function in turn returns the length of the string .While
calculating the length it doesn't count '\0'. Even in the
second call,
len2 = strlen("Humpty Dumpty");
What gets passed to strlen() is the address of the
string and not the string itself.

User-defined function for strlen():

main()
{
char arr[] = "Bamboozled";
int len1,len2;
len1 = xstrlen(arr);
len2 = xstrlen("Humpty Dumpty");
printf("\nstring = %s length = %d", arr, len1);
printf("\nstring = %s length = %d", "Humpty Dumpty", len2);
}
xstrlen(char *s)
{
int length = 0;
while(*s! = '\0')
{
length++;
s++;
}
return (length);
}

output:
string = Bamboozled length = 10
string Humpty Dumpty length = 13

Share |

Labels:

0 comments:

Post a Comment