输入n个字符,统计其空格、英文字母、回车、其他字符的个数(用do while来做)
时间: 2023-06-04 14:06:23 浏览: 32
好的,这是一个计算问题,很高兴为您解答。以下是程序的实现代码:
```
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
int space = 0, letter = 0, returnn = 0, other = 0;
printf("请输入字符,以#结束:");
do
{
c = getchar();
if (c == ' ')
{
space++;
}//统计空格数量
else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
letter++;
}//统计英文字母数量
else if (c == '\n')
{
returnn++;
}//统计回车数量
else
{
other++;
}//统计其他字符数量
} while (c != '#');
printf("空格:%d 个,英文字母:%d 个,回车:%d 个,其他字符:%d 个\n", space, letter, returnn, other);
system("pause");
return 0;
}
```
注意,该代码只计算输入的n个字符中出现的空格、英文字母、回车、其他字符的个数,并不包括输入的n个字符总数。如果需要统计字符总数,可以使用一个计数器来记录输入的字符个数,并在循环结束后输出。