Friday, July 6, 2012

strcmp implementation in C !!!

strcmp is another string library function which is used to compare two strings and returns zero if both strings are same , returns +ve value if first string is greater than second string and returns -ve value if second string is greater than first string.
How it works: Basically strcmp works in such a way that, it compares each character by character. i.e first character of the string with first character of the second string and second character of the string with second character of the second string etc. Like nth character of the first string with the nth character in the second string until it reaches end of any string. Here comparing character means comparing the ascii value of the character. When operators (+,-,<,>) applied to the characters, they will do operations on the ascii values.

strcmp implementation using arrays:
int strcmp_arry(char *src1, char *src2)
{
    int i=0;
    while((src1[i]!='\0') || (src2[i]!='\0'))
    {
        if(src1[i] > src2[i])
            return 1;
        if(src1[i] < src2[i])
            return -1;
        i++;
    }

    return 0;
}
strcmp implementation using pointers:
int strcmp_ptr(char *src1, char *src2)
{
    int i=0;
    while((*src1!='\0') || (*src2!='\0'))
    {
        if(*src1 > *src2)
            return 1;
        if(*src1 < *src2)
            return -1;
        src1++;
        src2++;
    }
    return 0;
}

Below is the complete working C code implementation for the strcmp string function.

#include<stdio.h>
#include<string.h>

//using arrays , need to move the string using index
int strcmp_arry(char *src1, char *src2)
{
    int i=0;
    while((src1[i]!='\0') || (src2[i]!='\0'))
    {
        if(src1[i] > src2[i])
            return 1;
        if(src1[i] < src2[i])
            return -1;
        i++;
    }

    return 0;
}
//using pointers, need to move the position of the pointer
int strcmp_ptr(char *src1, char *src2)
{
    int i=0;
    while((*src1!='\0') || (*src2!='\0'))
    {
        if(*src1 > *src2)
            return 1;
        if(*src1 < *src2)
            return -1;
        src1++;
        src2++;
    }
    return 0;
}

main()
{
    char amessage[] = "string";
    char bmessage[] = "string1";
    printf(" value is %d\n",strcmp_arry(amessage,bmessage));
    printf(" value is %d\n",strcmp_ptr(amessage,bmessage));
}

8 comments:

Anonymous said...

your while loop is wrong, needs to use && instead of ||

Chanduthedev p said...

it should be || only because, if any one of the two strings, ends, we need to stop the loop and need to return there itself. no need to go further. Let me know if you still have doubt. anyway thanks for your comment.

Unknown said...

Thanks for the article... I understood it perfectly after reading your blog.. Good work bro :)

Chanduthedev p said...

Thanks for your comments.

Anonymous said...

Hello,

As you said, the conidtion to stop the loop is "any one of the the two strings ends", and that means

(*src1=='\0') || (*src2=='\0')

so the condition to make the loop keep going is "no strings ends", that is, the inverse of the above statement should be:

(*src1!='\0') && (*src2!='\0')

Anonymous said...

This is wrong solution.
what if I compare "Hello" and "Hello Dude".
Your function will return 0, which is wrong. Make sure next time, add more test cases to your code.

Regards,

Chanduthedev p said...

there is no issue with the code, it will work perfectly with || operator in the while loop. I tested with all the possibilities. like
"hello" "hello world"
"hello world" "hello"
"hello world" "hello world"

Anonymous said...

An implementation of the goal has been done for the use of the goals for the people. All the ideas of the success and custom writing help has been accentuated for the full use of the norms for all ideal and fantastic matters.

Popular Posts