请用数组法编写一个程序,输入一个由小写字母和其他字符构成的序列保存在tt中,统计“a’”到“z”26个字母各自出现的次数,并依次保存在pp数组中,并输出pp数组内容。
时间: 2024-04-29 08:23:37 浏览: 10
```python
tt = input("请输入一个由小写字母和其他字符构成的序列:")
pp = [0]*26
for i in tt:
if i.islower():
pp[ord(i)-ord('a')] += 1
print(pp)
```
解释:
1. 首先输入一个由小写字母和其他字符构成的序列,并将其保存在变量tt中。
2. 创建一个长度为26,元素都为0的数组pp,用于统计每个字母出现的次数。
3. 遍历tt中的每个字符,判断该字符是否为小写字母,如果是,则将pp中对应字母的计数器加1。
4. 最后输出pp数组的内容。由于pp数组下标从0开始,因此需要将字符转换成对应的数组下标,即ord(i)-ord('a')。
相关问题
编写一个程序,输入一个由小写字母和其他字符构成的序列保存在tt中,统计 “a”到“z”26个字母各自出现的次数,并依次保存在pp数组中,并输出pp数组内容。文件名保存为7-3-4.c。
### 回答1:
以下是7-3-4.c的代码:
#include <stdio.h>
int main() {
char tt[100];
int pp[26] = {0};
int i, j;
printf("请输入一个由小写字母和其他字符构成的序列:\n");
scanf("%s", tt);
for (i = 0; tt[i] != '\0'; i++) {
if (tt[i] >= 'a' && tt[i] <= 'z') {
pp[tt[i] - 'a']++;
}
}
printf("各个字母出现的次数为:\n");
for (j = 0; j < 26; j++) {
printf("%c: %d\n", 'a' + j, pp[j]);
}
return 0;
}
当输入一个由小写字母和其他字符构成的序列后,程序会统计出其中每个字母出现的次数,并依次保存在pp数组中,最后输出pp数组内容。
### 回答2:
您好!以下是用C语言编写的实现要求的程序:
#include <stdio.h>
#include <ctype.h>
int main() {
char tt[300]; // 保存输入的序列
int pp[26] = {0}; // 保存每个字母出现的次数
// 输入序列
printf("请输入一个由小写字母和其他字符构成的序列(最多300个字符):");
fgets(tt, sizeof(tt), stdin);
// 统计字母出现次数
for (int i = 0; tt[i]!='\0'; i++) {
char c = tolower(tt[i]);
if (c >= 'a' && c <= 'z') {
pp[c - 'a']++;
}
}
// 输出统计结果
for (int i = 0; i < 26; i++) {
printf("字母 %c 出现次数:%d\n", 'a' + i, pp[i]);
}
return 0;
}
运行程序后,会提示您输入一个由小写字母和其他字符构成的序列。程序会统计该序列中每个字母出现的次数,并将结果保存在pp数组中。最后,程序会依次输出pp数组中每个字母出现的次数。
希望对您有所帮助!
### 回答3:
#include <stdio.h>
int main() {
char tt[300];
int pp[26] = {0};
printf("请输入一个序列:");
scanf("%s", tt);
for (int i = 0; tt[i] != '\0'; i++) {
if (tt[i] >= 'a' && tt[i] <= 'z') {
pp[tt[i] - 'a']++;
}
}
printf("字母个数统计如下:\n");
for (int i = 0; i < 26; i++) {
printf("%c:%d次\n", 'a' + i, pp[i]);
}
return 0;
}
用c语言编写一个程序,在程序中输入字符串,统计其中大写字母、小写字母和数字字符的个数
好的,下面是用 C 语言编写的程序,可以统计输入字符串中大写字母、小写字母和数字字符的个数:
```c
#include <stdio.h>
#include <ctype.h>
int main(void) {
char str[100];
int upper = 0, lower = 0, digit = 0;
// 读取字符串
printf("请输入一个字符串:");
fgets(str, 100, stdin);
// 统计字符数量
for (int i = 0; str[i] != '\0'; i++) {
if (isupper(str[i])) {
upper++;
} else if (islower(str[i])) {
lower++;
} else if (isdigit(str[i])) {
digit++;
}
}
// 输出结果
printf("大写字母个数:%d\n", upper);
printf("小写字母个数:%d\n", lower);
printf("数字字符个数:%d\n", digit);
return 0;
}
```
程序中先定义了三个变量 `upper`、`lower` 和 `digit` 分别用于统计大写字母、小写字母和数字字符的个数。然后使用 `fgets()` 函数读取用户输入的字符串,遍历字符串中的每个字符,并使用 `isupper()`、`islower()` 和 `isdigit()` 函数判断字符是否为大写字母、小写字母或数字字符,然后分别对应地增加相应的计数器。最后通过 `printf()` 函数输出统计结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)