TopBottom

Followers



Click on more
SUBSCRIBE

Enter your email address:

Delivered by FeedBurner



VIDEO

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

strcmp() function

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

This is a function which compares two strings to fin
out whether they are same or different.The two strings are
compared character by character until there is a mismatch
or end of one of the strings is reached, whichever occurs
first. If the two strings are identical , strcmp() returns
a value zero. If they aren't not, it returns the numeric
difference b/w the ASCII values of the first non-matching
pairs of characters. Here is a program which puts strcmp()
in action.

main()
{
char string1[] = "Jerry";
char string2[] = "Ferry";
int i, j, k;
i = strcmp ( string1, "Jerry");
j = strcmp ( string1, "string2");
k = strcmp ( string1, "Jerryboy");
printf("\n%d%d%d",i,j,k);
}

output:
0 4 -32

In the first call to strcmp(), the two strings are
identical-"Jerry" and "Jerry" and the value returned by
strcmp() is zero. In the second call ,the first character
of "Jerry" doesn't match with the first character of
"Ferry" and the result is 4, which is the numeric
difference b/w ASCII value of 'J' and ASCII value of 'F''.
In the third call to strcmp() also the same procedure
repeats.

Share |

Labels:

0 comments:

Post a Comment