Saturday, November 30, 2019

Function: strcmp()

C/C++ Programing:

strcmp( leftString[], rightString[] ) 

This function can return three different integer values based on the comparison:

1. Zero

2. An integer greater than zero

3. An integer less than zero.

......................................................................

Code:

int main()

      
    char leftStr[] = "g f g";
    char rightStr[] = "g f g";
      
    // Using strcmp()
    int res = strcmp(leftStr, rightStr);
      
    if (res==0)
        printf("Strings are equal");
    else 
        printf("Strings are unequal");
      
    printf("\nValue returned by strcmp() is:  %d" , res);
    return 0;
}
.........................................................................

Output: when both string are same it will return zero.

Strings are equal
Value returned by strcmp() is:  0


Source

No comments:

Post a Comment