int main() { char str[3][80]; int upper = 0, lower = 0, chinese = 0, digit = 0, space = 0, others = 0; int i, j; for (i = 0; i < 3; i++) { gets(str[i]); for (j = 0; j < 80 && str[i][j] != '\0'; j++) { if (str[i][j] >= 'A' && str[i][j] <= 'Z') { upper++; } else if (str[i][j] >= 'a' && str[i][j] <= 'z') { lower++; } else if (str[i][j] >= '0' && str[i][j] <= '9') { digit++; } else if (str[i][j] == ' ') { space++; } else if (str[i][j] >= 128) { chinese++; } else { others++; } } } printf("upper:%d\n", upper); printf("lower:%d\n", lower); printf("chinese:%d\n", chinese/3); printf("digit:%d\n", digit); printf("space:%d\n", space); printf("others:%d\n", others); return 0; }
时间: 2023-06-23 18:10:27 浏览: 48
这是一个统计输入的三行字符串中各种字符出现次数的程序。代码中定义了一个字符数组 `str`,用来存储输入的三行字符串;以及六个变量 `upper`、`lower`、`chinese`、`digit`、`space`、`others`,分别用来存储大写字母、小写字母、中文字符、数字、空格和其他字符的出现次数。
程序使用了两层循环,外层循环控制输入三行字符串的过程,内层循环遍历每个字符串的每个字符,根据字符的种类更新相应计数器的值。具体而言,如果是大写字母,则 `upper` 计数器加一;如果是小写字母,则 `lower` 计数器加一;如果是数字,则 `digit` 计数器加一;如果是空格,则 `space` 计数器加一;如果是非 ASCII 字符,则 `chinese` 计数器加一;否则,说明是其他字符,`others` 计数器加一。
最后输出各种字符出现的次数即可。需要注意的是,由于输入的三行字符串长度不一定相等,因此内层循环需要在判断字符不为 '\0' 时终止。另外,中文字符的判断使用了一个简单的近似方法,即将中文字符的数量除以 3,这是因为中文字符在 ASCII 编码中占据了三个字节。
相关问题
digits = [int(digit) for digit in str(num)] 什么意思
这段代码是将一个整数转换为一个由每个数字组成的列表。具体来说,代码中的 `str(num)` 将整数 `num` 转换为字符串,然后 `for` 循环遍历该字符串中的每个字符,`int(digit)` 将字符转换为整数,并将其添加到列表 `digits` 中。最终,`digits` 列表包含了整数 `num` 中每个数字的单独的整数形式。例如,如果 `num` 是 12345,则 `digits` 将是 `[1, 2, 3, 4, 5]`。
程序改错 功能:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 #include <stdio.h> void main(void) { char str[80]; int i; int letters=0,space=0,digit=0,others=0; printf("please input a string:\n"); get(str); for(i=0;str[i]!='\0';i++) { if(str[i]>='a'&&str[i]<=z'&&str[j]>='A'&&str[i]<=Z) letters++; else if(str[i]=!'") space++; else if(str[i]>='0'&&str[i]<='9') digit++; else others++; } printf("char=%d space=%d digit=%d others=%d\n",letters,space,digit,others); }
程序有以下几处错误:
1. `get()` 函数应该改为 `gets()` 函数。
2. 第10行的 `str[j]` 应该改为 `str[i]`。
3. 第12行的判断条件应该是 `str[i]!=' ' && str[i]!= '\t'`,即判断字符不是空格也不是制表符。
4. 第13行的判断条件应该是 `str[i]>='0'&&str[i]<='9'`,即判断字符是数字。
修改后的程序如下所示:
```c
#include <stdio.h>
void main(void) {
char str[80];
int i;
int letters=0,space=0,digit=0,others=0;
printf("please input a string:\n");
gets(str);
for(i=0;str[i]!='\0';i++) {
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
letters++;
else if(str[i]!=' ' && str[i]!= '\t')
space++;
else if(str[i]>='0'&&str[i]<='9')
digit++;
else
others++;
}
printf("char=%d space=%d digit=%d others=%d\n",letters,space,digit,others);
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)