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]

8 comments:

RAJARAJAN said...

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

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.

Anonymous said...

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

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

AMIT CHATURVEDI said...

Good job

AMIT CHATURVEDI said...

Good job

jeevan reddy mandali 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;
}

jeevan reddy mandali 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;
}