1、问题及代码:
#include<stdio.h>
int main()
{
char c;
int n=0;
while (c=getchar()!='\n')
n++;
printf("有%d个字母\n",n);
return 0;
}
运行结果:
知识总结:
getchar()的运用
学习心得:
在!=后面的\n用了双引号导致错误。
2、问题及代码:for循环
#include<stdio.h>
int main()
{
char c;
int n;
for (n=0;(c=getchar())!='\n';n++)
putchar(c+4);
printf("\n");
printf("有%d个字母\n",n);
return 0;
}
运行结果:
知识总结:
for循环解决
学习心得:
c=getchar()没有再将其括号起来,导致运行出错。
3、问题及代码:
#include <stdio.h>
int main()
{
char c;
int n=0;
printf("输入一个句子: ");
while((c=getchar())!='\n')
{
if(c>='0'&&c<='9') n++;
}
printf("总共有 %d 个数字.\n", n);
return 0;
}
运行结果:
知识总结:
while与if合用
学习心得:
只有0到9是数字,其余都是字母或符号。