0
#include<stdio.h>
#include<string.h>
void main()
{
    char str[20],cmp[20];
    int x;
    printf("enter the string :  ");
    gets(str);
    printf("enter the string 2 : ");
    gets(cmp);
    x=strcmp(str,cmp);
    printf("%d",x);
}

my doubt is how strcmp returns an integer ??how will it take the words

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by deceptikon
1

strcmp returns the difference of the two strings. If the first string is "less" than the second string it returns a value less than 0. If the first string is "greater" than the second string it returns a value greater than 0. And if the two strings are identical, it returns 0.

Here's an example implementation:

int my_strcmp(char const* a, char const* b)
{
    // Find the first two characters that don't match
    while (*a == *b && *a)
    {
        ++a;
        ++b;
    }

    // Compare the mismatched characters and return the absolute difference
    if (*a < *b)
    {
        return -1;
    }
    else if (*a > *b)
    {
        return +1;
    }
    else
    {
        return 0;
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.