不用库函数,比较字符串strcmp

比较字符strcmp

  1. 定义:按字典排序法,比较两个字符串的值
  2. 函数原型:int StrCmp(const char *str1, const char *str2);

下面展示裁判程序

#include 
#include 

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

int main()
{
    char a[1024], b[1024];
    int r;
    gets(a);
    gets(b);
    r = StrCmp(a, b);
    puts(!r ? "a = b" : r > 0 ? "a > b" : "a < b");
    return 0;
}

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

我的代码

int StrCmp(const char *str1, const char *str2)
{
	int i = 0, t;
	while( !( t = str1[i] - str2[i] ) && str1[i] && str2[i] )
	{
		++i;
	}
	
	return t;	
} 

知识点&&代码闪光点

1.while语句真是使用的活灵活现,当满足两个字符串没有结束时,以及字符串相等时,++i,若字符不相等,将比较的值传回主函数,这个while我佩服的五体投地(这个代码是一个学霸给我看的,我就借用了,太牛逼了)

你可能感兴趣的:(暑期集训总结)