2016年7月5日 星期二

[C] String comparison

#include<stdio.h>
#include<string.h>
#define IN

#define TRUE  1
#define FALSE 0

int  Stricmp(
    IN char* firstStr,
    IN char* secStr)
{
    int firstLen, secLen;
    int  i = 0;

    if(firstStr == NULL)
    {
        if(secStr == NULL)
            return TRUE;
        else
            return FALSE;
    }

    firstLen = strlen(firstStr);
    secLen   = strlen(secStr);

    if(firstLen != secLen)
        return FALSE;

    while (firstStr[i] != '\0')
    {
        if((firstStr[i] != secStr[i]) &&
                /* Interesting Part */
                ((firstStr[i]-secStr[i])%('a'-'A') != 0))
            return FALSE;
        else
            ++i;
    }
    return TRUE;
}

int main()
{
    char a[]="happy";
    char b[]="happy1";

    printf("String : %s", Stricmp(a,b)?"same":"different");
}

0 意見:

張貼留言