【发布时间】:2017-02-13 16:10:29
【问题描述】:
使用 cprogrammingsimplified 教程编写我自己的字符串比较。 完成重新格式化并运行它。 适用于单个单词,
但是
键入空格键会跳过第二次扫描并立即输出 '字不一样'
有人知道如何允许使用单个空格键吗?
提前致谢。
#include <stdio.h>
int mystrcmp(char s1[], char s2[]);
int main(){
char s1[10], s2[10];
int flag;
printf("Type a string of 10\n\n");
scanf("%s",&s1);
printf("type another string of 10 to compare\n\n");
scanf("%s",&s2);
flag = mystrcmp(s1,s2);
if(flag==0)
printf("the words are the same\n\n");
else
printf("the words are not the same\n\n");
return 0;
}
int mystrcmp(char s1[], char s2[]){
int l=0;
while (s1[l] == s2[l]) {
if (s1[l] == '\0' || s2[l] == '\0')
break;
l++;
}
if (s1[l] == '\0' && s2[l] == '\0')
return 0;
else
return -1;
}
【问题讨论】:
-
请给出两个似乎不起作用的示例输入
-
空格是scanf函数的分隔符。所以只有第一个单词会放在 s1 中。
-
非常感谢大家,只是在其他任务中,所以必须在这里返回所有额外的信息!
标签: c string-comparison