(c语言)比较两个字符串的大小

来源:互联网 发布:西门子模拟量编程实例 编辑:程序博客网 时间:2024/05/20 22:02

#include <stdio.h>

 

#define N 100

 

int input( char *a, char *b )                   //输入两个字符串

{

    printf("Input the first information:\n");

    fgets(a,N,stdin);

    printf("Input the secend information:\n");

    fgets(b,N,stdin);

}

 

int my_strcmp( char *a, char *b )                //比较字符串每个字符的大小

{

    while( (*a != '\0') && (*b != '\0') )

    {

        if( *a > *b)

{

            return 0;

        }

else if( *a < *b )

{

            return 1;

}

else

{

        a++;

        b++;

}

    }

 

    if((*a == '\0') && (*b != '\0'))             //字符串b比字符串a长

    {

        return 1;

    }

    else if((*a != '\0') && (*b == '\0'))        //字符串a比字符串b长

    {

        return 0;

    }

    else

    {

        return 2;

    }

 

}

 

 

int main()

{   

    char a[N] = {0};

    char b[N] = {0};

    int net2 = 0;

 

    input(a,b);                                  //调用输入函数

    net2 = my_strcmp(a,b);              //调用比较大小函数

    

    if( 0 == net2)                               //输出大小

    {

        printf("a > b\n");

    }

    else if(1 == net2)

    {

        printf("a < b\n");

    }

    else

    {

        printf("a = b\n");

    }

 

    return 0;

}

0 0