6-8 '字符串04-比较字符串(10 分)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41611106/article/details/82595401

C语言标准函数库中包括 strcmp 函数,用于字符串的比较。作为练习,我们自己编写一个功能与之相同的函数。
函数原型

// 比较字符串
int StrCmp(const char *str1, const char *str2);

说明:str1 和 str2 分别为两个字符串的起始地址。按字典排序法,若 str1 串值大于 str2,则函数值为正整数;若 str1 串值小于 str2,则函数值为负整数;若 str1 串值与 str2 相等,则函数值为0。
裁判程序

#include <stdio.h>
// 比较字符串
int StrCmp(const char *str1, const char *str2);

int main()
{
    char a[1024], b[1024];
    int n;
    gets(a);
    gets(b);
    n = StrCmp(a, b);
    if (n > 0)
    {
        puts("a > b");
    }
    else if (n < 0)
    {
        puts("a < b");
    }
    else
    {
        puts("a = b");
    }
    return 0;
}

/* 你提交的代码将被嵌在这里 */

输入样例

stock
stack

输出样例

a > b

int StrCmp(const char *str1, const char *str2)
{
    int n;
    n=strcmp(str1,str2);
    return n;
}

猜你喜欢

转载自blog.csdn.net/qq_41611106/article/details/82595401