计算字符串中的元音数
Counting the number of vowels in a string
我正在编写一个返回给定字符串的元音数的函数,这是代码:
int isVowel(string sequence)
{
int numberOfVowels = 0; //Initialize number of vowels to zero
string vowels = "aeiouAEIOU"; //Possible vowels, including capitals
for (int i = 0; i <= sequence.length(); i++)
{
for (int j = 0; j <= vowels.length(); j++)
{
if (sequence[i] == vowels[j])
{
numberOfVowels += 1;
}
}
}
return numberOfVowels;
}
这将返回答案。例如," a"的输入返回2," aa"的输入返回3,等等
i <= sequence.length()
<=
几乎在循环中永远都不正确,因为C 使用了0基于0的索引。相反,您应该做
i < sequence.length()
相关文章:
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 通过字符串来计算长度需要更长的时间,而不是移动字符串几次?
- 如何计算特定字母在字符串中出现的次数?(C++)
- 计算在同一位置至少包含一个常用字符的不同字符串对
- 给定一个单词数组和一个字符串,如何计算给定字符串中的所有单词
- 使用标准库计算字符串中的十进制数
- 如何计算字符串中用户输入元素的出现次数?
- 如何在 C++ 中计算字符串的算术运算
- 计算字符串中子字符串的出现次数
- 用于计算字符串中元音的函数
- 当 NUL 字符被定义为字符串的一部分时,为什么 strlen() 不计算终止 NUL 字符的字节?
- 计算表达式字符串由 std::map 中的键组成
- 如何计算字符串 c++ 中的"xxx"数?
- 通过将文本文件读取为字符串/向量来计算加权/未加权 GPA
- 如何更有效地计算n个字符串之间的不匹配分数?
- 将字符串计算为 C++ 中的表达式
- 在C++中从字符串计算算术表达式
- 在字符串计算后添加空字符
- C++使用C字符串计算文件中的字符、单词、句子
- 不使用字符串计算数字中的数字