制作一个程序来计算字母表中每个字母的出现次数
Making a program that counts the amount of occurrences for each letter in the alphabet issues
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
string text;
int i;
char x;
int n = 0;
int main(int argc, const char * argv[])
{
cout << "Enter some text: " << endl;
getline(cin, text);
for (x = 'a'; x <= 'z'; x++)
{
for (i = 0; i<= text.length(); i++)
{
if (text[i] == x)
{
n++;
cout << x << ": " << n << endl;
}
}
}
}
所以我希望它打印出字符串中出现的每个字母的出现次数,但相反,我得到这个:
输入:你好
输出:
e: 1
h: 2
l: 3
l: 4
o: 5
我理解为什么每次找到字母时它都会计数,但是我如何让它显示每个字母实际出现的次数。例如,这就是我想说的:
e: 1
h: 1
l: 2
o: 1
在每个循环(x(的开头,你应该将n重置为0。
您还应该移动语句
cout << x << ": " << n << endl;
从第二个循环到第一个循环的结束
相关文章:
- 不计算一个范围内的完美数
- 计算一个单词在映射中出现的次数 - 使用 transform() - 调用不匹配
- C++在不使用pow或循环的情况下计算一个数字的幂
- 使用递归计算一个函数中的高度和大小
- 使用 c++ 特征库中的特征向量() 只计算一个特征向量
- 我如何计算一个单词在数组中重复的次数
- 计算一个字符串的每个字符在另一个字符串中的出现次数
- 如何计算一个数字在数组中出现的次数
- 如何计算一个数字到另一个数字中的数量
- 计算一个字符在数组中出现的次数
- 计算一个班级的人数
- qt多线程:计算一个积分
- 使用C++计算一个非常大的幂
- 计算一个数字的大幂
- 有没有一个函数可以计算一个向量中所有的正数
- 如何用3个用户定义的函数计算一个数字的真平方根
- 计算一个表达式来计算循环
- 并行计算一个大矢量的和
- 计算一个数字的质因数
- 计算一个*一个没有溢出的模数n