試圖讓scanf
迭代和評估字符串的每個部分與isdigit
。但它似乎在跳過第一個'塊',從而抵消了一切。關於我在做什麼錯誤的建議?scanf在while循環中不評估第一個信息塊
int main (void) {
int icount = 0;
int c = 0;
int compare = 0;
int len = 0;
char s[256] = "";
printf("Enter a string:\n\n");
while (scanf("%s", s) == 1) {
scanf("%255s", s);
len = strlen(s);
printf("the string is %d characters long\n", len);
while (len > 0) {
printf("c is on its s[%d] iteration\n", c);
if (isdigit(s[c])) {
compare = compare + 1;
}
c++;
len--;
}
if (compare == strlen(s)) {
icount = icount + 1;
}
c++;
}
printf("\ni count is %d\n", icount);
return 0;
}
當我運行它,我不斷收到回數據是這樣的:
./a
Enter a string:
17 test 17
the string is 4 characters long
c is on its s[0] iteration
c is on its s[1] iteration
c is on its s[2] iteration
c is on its s[3] iteration
the string is 2 characters long
c is on its s[5] iteration
c is on its s[6] iteration
i count is 0
爲什麼你'scanf()'兩次,是否是一個錯字?我的意思是複製粘貼問題。 –
在while(scanf(...)== 1){'?')後面移除'scanf()我想,你還需要在循環的某個地方將'c'重置爲0。 –
不是不是一個錯字我認爲我必須使用第一個scanf作爲我的條件,第二個作爲循環內實際發生的事情? – sandyman