Saturday, July 7, 2007

Implement the strcmp(str1, str2) function.

There are many ways one can implement the strcmp() function. Note that strcmp(str1,str2) returns a -ve number if str1 is alphabetically above str2, 0 if both are equal and +ve if str2 is alphabetically above str1.

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]

10 comments:

RAJARAJAN said...

thanks...............

September 7, 2011 at 1:40 PM
Uttam Agrawal said...

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.

December 24, 2011 at 11:56 PM
Anonymous said...

Nop, this code doesn' t work is s1 is a prefix string of s2.

December 18, 2012 at 6:26 AM
Anonymous said...

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

March 30, 2013 at 1:46 AM
Unknown said...

Good job

July 25, 2013 at 7:42 PM
Unknown said...

Good job

July 25, 2013 at 7:46 PM
Unknown said...

Try this one for better understanding of how actual strcmp() works:
#include
#include

int main(void)
{
char string1[20], string2[20];
char substring[20], *ret;
int i=0,len=0, count=0;
puts("enter the stirng one to compare");
fgets(string1, sizeof(string1), stdin);
len = strlen(string1);
if(string1[len-1]=='\n')
string1[len-1]='0円';

puts("enter the stirng two to compare");
fgets(string2, sizeof(string2), stdin);
len = strlen(string2);
if(string2[len-1]=='\n')
string2[len-1]='0円';
if(strlen(string1)==strlen(string2))
{
for(i=0;string1[i]!='0円', string2[i]!='0円', i0)
printf("string2 is less than string1");
}

if(strlen(string1)0)
printf("string2 is less than string1");
}

if(strlen(string1)>strlen(string2))
{
for(i=0;string2[i]!='0円', i0)
printf("string2 is less than string1");
}


return 0;
}

September 5, 2014 at 1:05 PM
Unknown said...

Try this one for better understanding of how actual strcmp() works:
#include
#include

int main(void)
{
char string1[20], string2[20];
char substring[20], *ret;
int i=0,len=0, count=0;
puts("enter the stirng one to compare");
fgets(string1, sizeof(string1), stdin);
len = strlen(string1);
if(string1[len-1]=='\n')
string1[len-1]='0円';

puts("enter the stirng two to compare");
fgets(string2, sizeof(string2), stdin);
len = strlen(string2);
if(string2[len-1]=='\n')
string2[len-1]='0円';
if(strlen(string1)==strlen(string2))
{
for(i=0;string1[i]!='0円', string2[i]!='0円', i0)
printf("string2 is less than string1");
}

if(strlen(string1)0)
printf("string2 is less than string1");
}

if(strlen(string1)>strlen(string2))
{
for(i=0;string2[i]!='0円', i0)
printf("string2 is less than string1");
}


return 0;
}

September 5, 2014 at 1:05 PM
Anonymous said...

check here Louis Vuitton fake Bags try here best replica designer bags great site high replica bags

May 20, 2022 at 4:43 AM
Anonymous said...

my website get more anchor Full Report Clicking Here his response

June 15, 2022 at 1:25 AM

Post a Comment

Subscribe to: Post Comments (Atom)

AltStyle によって変換されたページ (->オリジナル) /