C Programming/C Reference/nonstandard/strncasecmp

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

C programming language offers a function called strncasecmp that compares the first n characters of a string with the other irrespective of the case of characters. strncasecmp is found in string.h header file.


 int strncasecmp(const char *f1, const char *f2, size_t n );

The return value of this function is same as that of strcasecmp i.e it returns 0 if f1 and f2 are equal, a positive integer if f1 is found greater than f2 or a negative integer if f1 is found less than f2.


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

int main(int argc, char *argv[]){
    int i;
    if(argc != 4){
        printf("bad input\n");
        return 1;
    i = strncasecmp(argv[1], argv[2], atoi(argv[3]));
    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;