Monday, 15 April 2013

STRCASECMP : Implementation in C
#include <stdio.h>

int mytoupper(char c){
    // 'A' = 65
    // 'a' = 97
    if(('a'<=c) && (c <= 'z'))
        c = 'A' + (c - 'a');
    return c;
}

int mytolower(char c){
    // 'A' = 65
    // 'a' = 97
    if(('A' <= c) && (c <= 'Z'))
        c = 'a' + (c - 'A');
    return c;
}

int mystrcasecmp(const char *s1, const char *s2){
    while((mytoupper(*s1)==mytoupper(*s2)) && *s1 && *s2) s1++, s2++;
    return *s1-*s2;
}

int main(){
    char *a = "Umesh";
    char b[] = "uMeSh";
    int diff = mystrcasecmp(a,b);
    if(diff==0)
        printf("Equal\n");
    else if(diff < 0)
        printf("Less than\n");
    else
        printf("Greater\n");
    return 0;
}

// Output: Equal

No comments:

Post a Comment