1. (简答题) Write a program to count the occurrences of each letter in an array of characters. Here is a sample run. ------------------------------------------------------ Enter the string : abcddcabae a occurs 3 times in the string, b occurs 2 times in the string, c occurs 2 times in the string, d occurs 2 times in the string, e occurs 1 times in the string,
时间: 2023-08-15 11:21:02 浏览: 150
计算字符中不同字符出现的次数
以下是 Python 代码实现,可以用来统计一个字符数组中每个字符的出现次数:
```python
string = input("Enter the string: ")
freq = {}
for char in string:
if char in freq:
freq[char] += 1
else:
freq[char] = 1
for char, count in freq.items():
print(char, "occurs", count, "times in the string.")
```
运行结果:
```
Enter the string: abcddcabae
a occurs 3 times in the string.
b occurs 2 times in the string.
c occurs 2 times in the string.
d occurs 2 times in the string.
e occurs 1 times in the string.
```
阅读全文