输入一个正整数 n ,再输入 n 个字符,分别统计出其中回车、数字字符和其他字符的个数。要求使用 switch 语句编写。
时间: 2023-03-16 10:44:10 浏览: 130
根据你提供的信息,可以使用如下代码编写: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);
相关问题
用C语言写出输入1个正整数n,在输入n个字符,分别统计其中英文字母、空格或回车、数字字符和其他字符的个数
好的,下面是代码:
```c
#include <stdio.h>
int main() {
int n, i;
char c;
int letters = 0, spaces = 0, digits = 0, others = 0;
printf("请输入正整数n:");
scanf("%d", &n);
printf("请输入%d个字符:\n", n);
for (i = 0; i < n; i++) {
scanf(" %c", &c);
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
letters++;
} else if (c == ' ' || c == '\n') {
spaces++;
} else if (c >= '0' && c <= '9') {
digits++;
} else {
others++;
}
}
printf("英文字母的个数:%d\n", letters);
printf("空格或回车的个数:%d\n", spaces);
printf("数字字符的个数:%d\n", digits);
printf("其他字符的个数:%d\n", others);
return 0;
}
```
代码说明:
1. 首先输入正整数n,表示接下来要输入n个字符。
2. 循环输入n个字符,每次读入一个字符c。
3. 判断c属于哪一类:英文字母、空格或回车、数字字符、其他字符。分别累加对应计数器的值。
4. 最后输出各类字符的个数。注意要用空格分隔各个输出项,以免混淆。
统计英文字母、空格或换行、数字字符:输入一个正整数 n ,再输入 n 个字符,统计 其中英文字母、空格或回车、数字字符和其他字符的个数。
### 回答1:
题目描述:
输入一个正整数n,再输入n个字符,统计其中英文字母、空格或回车、数字字符和其他字符的个数。
解题思路:
遍历输入的n个字符,对每个字符进行判断,如果是英文字母、空格或回车、数字字符或其他字符,就分别对应计数器加1。
代码实现:
n = int(input())
letter_count =
space_count =
digit_count =
other_count =
for i in range(n):
c = input()
if c.isalpha():
letter_count += 1
elif c.isspace():
space_count += 1
elif c.isdigit():
digit_count += 1
else:
other_count += 1
print("字母个数:", letter_count)
print("空格或回车个数:", space_count)
print("数字个数:", digit_count)
print("其他字符个数:", other_count)
运行结果:
输入:
10
a
b
1
2
3
4
5
6
7
8
输出:
字母个数: 2
空格或回车个数:
数字个数: 8
其他字符个数:
输入:
20
Hello World!
12345
!@#$%
输出:
字母个数: 10
空格或回车个数: 2
数字个数: 5
其他字符个数: 3
### 回答2:
此题涉及到字符的分类与统计,可以使用计数器对每个类别的字符进行计数,最终输出答案。
首先,读入一个正整数n,用于表示接下来输入的字符数量。接着,用一个循环读入n个字符,对每个字符进行分类计数。我们可以将字符分为四类:英文字母、空格或回车、数字字符和其他字符。
对于英文字母,可以使用isalpha()函数判断一个字符是否为字母。如果是,则将计数器alpha_count加1。
对于空格或回车,可以使用isspace()函数判断一个字符是否为空格或回车。如果是,则将计数器space_count加1。
对于数字字符,可以使用isdigit()函数判断一个字符是否为数字。如果是,则将计数器digit_count加1。
对于其他字符,也可以通过以上三种判断都失败来确定。如果是,则将计数器other_count加1。
最终输出四种字符的个数即可。
以下是思路的示例代码:
```python
n = int(input("请输入字符数量:"))
alpha_count = 0
space_count = 0
digit_count = 0
other_count = 0
for i in range(n):
char = input()
if char.isalpha():
alpha_count += 1
elif char.isspace():
space_count += 1
elif char.isdigit():
digit_count += 1
else:
other_count += 1
print("英文字母数量:", alpha_count)
print("空格或回车数量:", space_count)
print("数字数量:", digit_count)
print("其他字符数量:", other_count)
```
输入示例:
```
请输入字符数量:15
a
B
1
2
3
4
5
6
7
8
9
0
(其中包括两个Tab键,会被当做空格计数)
\n
```
输出示例:
```
英文字母数量: 2
空格或回车数量: 3
数字数量: 10
其他字符数量: 0
```
由此可见输入的15个字符中,两个英文字母、三个空格或回车、十个数字,没有其他字符。
### 回答3:
首先,我们需要了解如何通过 ASCII 码来确定一个字符的种类:
- 英文字母:ASCII 码中,65 到 90 是大写字母 A 到 Z,97 到 122 是小写字母 a 到 z。
- 空格或回车:ASCII 码中,32 是空格符,10 是换行符。
- 数字字符:ASCII 码中,48 到 57 是数字字符 0 到 9。
- 其他字符:不属于以上三种字符的都是其他字符。
有了这些基础知识,我们可以写出一个程序来统计字符的种类。
首先,读取输入的正整数 n,并循环输入 n 个字符。在输入的同时,我们可以判断每个字符的种类并累加计数器。最后输出计数器的值即可。
以下是 Python 代码示例:
```python
n = int(input())
num_alpha = num_space = num_digit = num_other = 0
for i in range(n):
s = input().strip()
for c in s:
if (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z'):
num_alpha += 1
elif c == ' ' or c == '\n':
num_space += 1
elif c >= '0' and c <= '9':
num_digit += 1
else:
num_other += 1
print("字母数量:", num_alpha)
print("空格或回车数量:", num_space)
print("数字字符数量:", num_digit)
print("其他字符数量:", num_other)
```
需要注意的是,以上代码仅适用于 ASCII 码中的字符。如果需要读取 Unicode 编码字符并统计,需要使用相应的函数和库,如 Python 中的 `unicodedata` 模块。