的strcmp_kr功能是基于字符串给K & R.'而' 循环字符串比较函数循环超过必要
#include<stdio.h>
#include<string.h>
int strcmp_kr (char *s, char *d) {
int i=0;
while ((s[i] == d[i])) {
printf("Entered while loop\n");
if (s[i] == '\0')
return 0;
i++;
}
return s[i] - d[i];
}
int main() {
char s1[15];
char s2[15];
printf("Enter string no. 1:");
scanf("%s", s1);
printf("Enter string no. 2:");
scanf("%s", s2);
strcmp_kr(s1, s2) == 0 ? printf("Strings equal!\n") : \
printf("Strings not equal by %d!\n", strcmp_kr(s1, s2));
}
输出比较功能:
$。 /a.out
输入字符串编号。 1:谦虚
输入字符串编号。 2:modesy
已输入而输入的循环
while循环
进入while循环
进入while循环
进入while循环
进入while循环
进入而循环
进入while循环
进入while循环
进入while循环
字符串不等于-5!
问题: 为什么while循环输入10次而不是5次?
不要使用'scanf()'而不指定字段宽度,否则它有缓冲区溢出的风险,但它并不完全关系到你的问题。我提到它是一种预防措施:) – 0decimal0