有人可以給我提供一些例子嗎?謝謝:)閱讀用戶輸入,直到遇到特定字符
#include <stdio.h>
int main()
{
int tcount = 0, ccount = 0, dcount = 0;
char ch;
printf("Enter your characters (! to end): \n");
/* What program code to write to get the result? */
printf("digits: %d\n", dcount);
printf("letters: %d\n", ccount);
return 0;
}
是用循環嗎?
for (tcount=0;tcount<10;tcount++)
{
scanf("%c",&ch);
if(ch == '!')
break;
}
測試結果:
你好5432用戶#
位數:4個字母:9
提示:'getchar()' –
您的任務是否將其限制爲最多10個字符的輸入?如果是這樣,for循環是適當的。如果不是,那麼一個while循環將是更合適的選擇。 –
除非您可以將輸入置於原始模式,否則用戶將不得不在輸入結果前按下輸入鍵。 – Jasen