有人可以给我提供一些例子吗?谢谢:)阅读用户输入,直到遇到特定字符
#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