比较字符strcmp
- 定义:按字典排序法,比较两个字符串的值
- 函数原型:int StrCmp(const char *str1, const char *str2);
下面展示裁判程序
#include <stdio.h>
#include <string.h>
// 比较字符串
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我佩服的五体投地(这个代码是一个学霸给我看的,我就借用了,太牛逼了)