文字列関数strcmp

2965 ワード

/*
*  :int strcmp(const char *s1,const char * s2);
*  :#include <string.h>
*  :     s1 s2。
*  :
*    s1<s2 ,   <0
*    s1=s2 ,   =0
*    s1>s2 ,   >0
*    strcmp
*/
#include <cstdio>
#include <cassert>

int _strcmp(const char * s,const char * t)
{
    assert((s != NULL)&&(t != NULL));
    while(*s && *t && *s==*t)
    {
        s++;
        t++;
    }
    return *s-*t;
}

int main()
{
    char str1[] = "apPle";
    char *str2 = "apple";

    int flag = _strcmp(str1,str2);

    if(flag < 0)    printf("%s is less than %s
",str1,str2); else if(flag == 0) printf("%s is equal %s
",str1,str2); else printf("%s is larger than %s
",str1,str2); getchar(); return 0; }