【发布时间】:2021-11-20 02:49:15
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char input[50];
int i, hello = 0; // Wish to out Yes if "9999" is input. But why does it not happen?
scanf("%s", input);
for (i = 0; i<strlen(input); i++){
if (input[i]>9 || input[i]<0){
hello = 1;}
}
if (hello == 0) printf("Yes\n");
else if (hello == 1)printf("No\n");
return 0;
}
【问题讨论】:
-
您的字符串包含字符,而不是相应的数字值。将
9更改为'9'并将0更改为'0'。这样你就可以进行字符比较。或者,您可以先将字符转换为对应的数字值,然后与普通数字值进行比较。
标签: c loops char c-strings digits