C Programming/C Reference/nonstandard/strcasecmp

From Wikibooks, open books for an open world
Jump to: navigation, search

In programming language C, strcasecmp is a function declared in the strings.h header file (or sometimes in string.h) that compares two strings irrespective of the case of characters.

This function is in POSIX.1-2001.

Prototype[edit]

int strcasecmp(const char *f1, const char *f2);


This function returns an integer i :
i > 0, if f1 is greater than f2
i < 0, if f1 is found less than string f2 and
i = 0, if f1 is equal to f2.

Example[edit]

# include <stdio.h>
# include <strings.h>
int main(int argc, char *argv[]){
 
    int i;
    if(argc != 3){
        printf("bad input\n");
        return 1;
    }
    i = strcasecmp(argv[1], argv[2]);
    if (i == 0)
        printf("'%s' equals '%s'\n", argv[1], argv[2]);
    else if (i > 0)
        printf("'%s' is greater than '%s'\n", argv[1], argv[2]);
    else if (i < 0)
        printf("'%s' is less than '%s'\n", argv[1], argv[2]);
    return 0;
}