📅  最后修改于: 2023-12-03 14:57:34.239000             🧑  作者: Mango
这个程序的目的是计算给定字符串的所有子字符串中出现的元音数量。元音字母包括 a、e、i、o 和 u。通过统计元音字母的数量,我们可以了解字符串中元音字母的分布情况,从而进行进一步的分析或处理。
你可以将以下代码片段添加到你的程序中:
def count_vowels(string):
vowels = 'aeiou'
count = 0
for i in range(len(string)):
for j in range(i, len(string)):
if string[j] in vowels:
count += 1
return count
input_string = input("请输入一个字符串:")
result = count_vowels(input_string)
print("字符串中所有子字符串中出现的元音数量为:", result)
输入:
请输入一个字符串:hello
输出:
字符串中所有子字符串中出现的元音数量为: 6
对于输入字符串 "hello",所有子字符串包括 "h"、"he"、"hel"、"hell"、"hello"、"e"、"el"、"ell"、"ello"、"l"、"ll"、"llo"、"l"、"lo"、"o"。其中出现的元音数量分别为 0、1、1、1、2、1、1、1、2、0、0、1、0、0、1,总计为 6。