问题:编写一个程序读取输入,读到#字符停止,然后报告读取的空格键,换行符数和所有其他字符的数量。
环境: vs2012
第一次代码:
#include<stdio.h>
#include<string.h>
int main(void)
{
int k=0,h=0,z=0; //k代表空格数,h代表换行符数,z代表其他字符
char ch; //读取的字符
printf("Please enter some char to total:\n");//提示语句
while((ch = getchar())!='# ')
{
if(ch== ' ')
k++;
else if(ch== '\n')
h++;
else
z++;
}
printf("spaces:%d,feed:%d,other:%d\n", k,h,z);
return 0;
}
虽然可以运行过去,但是总是在一直往下跑没有结果
经过仔细查找发现
while((ch = getchar())!='# ')中‘’有空格排除后 完美运行
#include<stdio.h>
#include<string.h>
int main(void)
{
int k=0,h=0,z=0; //k代表空格数,h代表换行符数,z代表其他字符
char ch; //读取的字符
printf("Please enter some char to total:\n");//提示语句
while((ch = getchar())!='#')
{
if(ch== ' ')
k++;
else if(ch== '\n')
h++;
else
z++;
}
printf("spaces:%d,feed:%d,other:%d\n", k,h,z);
return 0;