输入15个字符,统计并输出空格或回车、数字字符和其他字符的个数。要求使用switch语句编写。请注意,输入15个字符后,需回车表示输入结束,这最后一个回车表示输入结束,不统计在内。
时间: 2023-05-22 16:07:10 浏览: 35
好的,您好!请看以下代码:
```lua
local spaceCount = 0
local digitCount = 0
local otherCount = 0
for i=1,15 do
local input = io.read(1)
if input == '\n' then
break
end
local charCode = string.byte(input)
-- 统计空格、数字和其他字符
switch charCode do
case 32:
spaceCount = spaceCount + 1
break
case 48,49,50,51,52,53,54,55,56,57:
digitCount = digitCount + 1
break
default:
otherCount = otherCount + 1
break
end
end
print("空格个数:" .. spaceCount)
print("数字个数:" .. digitCount)
print("其他字符个数:" .. otherCount)
```
这个程序可以统计输入的15个字符中的空格、数字和其他字符的个数。当输入回车时,程序会停止并输出统计结果。
相关问题
输入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);