输入15个字符,统计并输出空格或回车、数字字符和其他字符的个数。要求使用switch语句编写。请注意,输入15个字符后,需回车表示输入结束,这最后一个回车表示输入结束,不统计在内。
时间: 2023-04-27 15:05:15 浏览: 56
请输入15个字符,用switch语句统计空格或回车、数字字符和其他字符的个数。请注意,最后一个回车表示输入结束,不统计在内。
```
int space_num = 0, digit_num = 0, other_num = 0;
for (int i = 0; i < 15; i++) {
char ch;
cin >> ch;
switch(ch) {
case ' ':
case '\n':
space_num++;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
digit_num++;
break;
default:
other_num++;
break;
}
}
cout << "空格或回车的个数是" << space_num << endl;
cout << "数字字符的个数是" << digit_num << endl;
cout << "其他字符的个数是" << other_num << endl;
```
相关问题
输入15个字符,统计并输出空格或回车、数字字符和其他字符的个数。要求使用switch语句编写。请注意,输入15个字符后,需回车表示输入结束,这最后一个回车表示输入结束,不统计在内
好的,以下是用中文回复:
可以使用switch语句编写一个程序,输入15个字符,统计并输出空格或回车、数字字符和其他字符的个数。需要注意的是,输入15个字符后,需要再输入一个回车表示输入结束,这个最后一个回车不统计在内。
具体实现可以参考以下代码:
```c++
#include <iostream>
using namespace std;
int main() {
int space_count = ; // 空格或回车的个数
int digit_count = ; // 数字字符的个数
int other_count = ; // 其他字符的个数
char ch;
for (int i = ; i < 15; i++) {
cin.get(ch); // 逐个读入字符
switch (ch) {
case ' ': // 空格
case '\n': // 回车
space_count++;
break;
case '': // 数字
case '1': // 数字1
case '2': // 数字2
case '3': // 数字3
case '4': // 数字4
case '5': // 数字5
case '6': // 数字6
case '7': // 数字7
case '8': // 数字8
case '9': // 数字9
digit_count++;
break;
default: // 其他字符
other_count++;
break;
}
}
cout << "空格或回车的个数:" << space_count << endl;
cout << "数字字符的个数:" << digit_count << endl;
cout << "其他字符的个数:" << other_count << endl;
return ;
}
```
这个程序会逐个读入15个字符,并根据字符的类型进行统计。最后输出空格或回车、数字字符和其他字符的个数。需要注意的是,最后一个回车不会被统计在内。
输入一个正整数 n ,再输入 n 个字符,分别统计出其中回车、数字字符和其他字符的个数。要求使用 switch 语句编写。
根据你提供的信息,可以使用如下代码编写:int enterNum = 0;
int digitNum = 0;
int otherNum = 0;
for(int i=0;i<n;i++){
char c = getchar();
switch(c){
case '\n':
enterNum++;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
digitNum++;
break;
default:
otherNum++;
break;
}
}
printf("回车字符的个数:%d\n",enterNum);
printf("数字字符的个数:%d\n",digitNum);
printf("其他字符的个数:%d\n",otherNum);
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)