Here are some C programs which implement the strcmp() function. This is also one of the most frequently asked interview questions. The prototype of strcmp() is
int strcmp( const char *string1, const char *string2 );
Here is some C code..
#include
int mystrcmp(const char *s1, const char *s2);
int main()
{
printf("\nstrcmp() = [%d]\n", mystrcmp("A","A"));
printf("\nstrcmp() = [%d]\n", mystrcmp("A","B"));
printf("\nstrcmp() = [%d]\n", mystrcmp("B","A"));
return(0);
}
int mystrcmp(const char *s1, const char *s2)
{
while (*s1==*s2)
{
if(*s1=='\0')
return(0);
s1++;
s2++;
}
return(*s1-*s2);
}
And here is the output...
strcmp() = [0]
strcmp() = [-1]
strcmp() = [1]
4 comments:
thanks...............
Sample C Program To Accept A String & Display It.
Sample C Program To Find The Length Of A String.
Sample C Program To Concatenate Two Strings.
Sample C Program To Compare Two Strings.
Sample C Program To Swap Two Strings.
Sample C Program To Swap Two Strings Using strcpy() Function.
Sample C Program To Sort A Given Number Of Strings Using strcmp() Function.
Sample C Program To Check Whether A String Is Palindrome Or Not.
Sample C Program To Print The Reverse Of A String.
Sample C Program To Join Two Strings.
Sample C Program To Display Array Of Strings.
Sample C Program To Convert String To An Integer Using atoi() Function.
Sample C Program To Accept A String & Display In Reverse.
Sample C Program To Accept A String & Display Its Alternate Characters.
Sample C Program To Accept A String & Display Alternate Characters In Either Case.
Nop, this code doesn' t work is s1 is a prefix string of s2.
Thanks a bunch for sharing this with all folks you
really recognise what you are speaking approximately!
Bookmarked. Please also discuss with my site =). We may
have a hyperlink trade contract among us
Look at my web page; waist to height ratio chart
Post a Comment