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:

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

    ReplyDelete
  2. 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

    ReplyDelete
  3. 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;
    }

    ReplyDelete
  4. 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;
    }

    ReplyDelete