简体   繁体   English

C ++中的字符计数器?

[英]Character Counter in C++?

I'm trying to find a way to display all the characters in a string and the number of times they occur. 我试图找到一种方法来显示字符串中的所有字符以及它们出现的次数。

This is what I have so far: 这是我到目前为止的内容:

//Any unused includes are part of the default code

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <string>

using namespace std;

int main()
{
    string st = "";

    cout << "Input a sentence: " << endl;
    getline(cin, st);
    int index = 0;
    int index2 = 0;
    int counters[26] = {0};
    for(int i = 0; i < st.length(); i++)
    {
        int counter = 0;
        index = st.find(st[i],0);
        for(int j = 0; j < st.length(); j++)
        {
            index2 = st.find(st[j]);
            if(index == index2)
            {
                counter++;
            }
        }
        cout << st[i] << ": " << counter << endl;
    }
    //cout << st[i] <<": " << counters[st[i] - 'a'] << endl;
    return 0;
}

and I return this: 我返回这个:

Input a sentence: 输入一个句子:

hello 你好

h: 1 小时:1

e: 1 e:1

l: 2 l:2

l: 2 l:2

o: 1 o:1

so I kind of have something but I can't figure out how to make the letters not repeat more than once. 所以我有一些东西,但是我不知道如何使字母不重复一次。 I know that I need to store them in an array but it's out of my ken. 我知道我需要将它们存储在一个数组中,但这已经超出了我的范围。

You were very close, nice try! 您非常亲密,不错的尝试! I liked the approach with the counters array, where every cell would represent the frequency of a letter in the given string. 我喜欢使用counters数组的方法,其中每个单元格都代表给定字符串中字母的频率。

So, just go and update this array, as this answer implies How to get character's position in alphabet in C language? 因此,只需更新此数组,因为此答案暗示着如何获得C语言中字母在字符中的位置? , without the plus one they mention there, since you want the index of the letter in your array. ,因为那里需要数组中字母的索引,所以没有在这里提到的加号。 In other words, for 'a', you need 0, 'b', you need 1 and so on. 换句话说,对于“ a”,您需要0,“ b”,您需要1,依此类推。

Then, in the printing phase, just use the above link's suggestion in the reverse way. 然后,在打印阶段,以相反的方式使用以上链接的建议。 When you print the i-th non-zero element of counters , print the i-th element of the element, which will reveal the letter in question. 当您打印counters的第i个非零元素时,请打印该元素的第i个元素,这将显示有问题的字母。

Putting all together, you get: 放在一起,您将获得:

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <string>

using namespace std;

int main()
{
    string st = "";

    cout << "Input a sentence: " << endl;
    getline(cin, st);
    int index = 0;
    int index2 = 0;
    int counters[26] = {0};
    for(size_t i = 0; i < st.length(); i++)
    {
        int counter = 0;
        index = st.find(st[i],0);
        for(size_t j = 0; j < st.length(); j++)
        {
            index2 = st.find(st[j]);
            if(index == index2)
            {
                counter++;
            }
        }
        counters[st[i] - 'a'] = counter; // update 'counters' array
    }
    for(int i = 0; i < 26; ++i)
        if(counters[i] != 0)            // print non-zero counters 
            cout << (char)(i + 'a') << ": " << counters[i] << endl;
    return 0;
}

Output: 输出:

e: 1
h: 1
l: 2
o: 1

I would do something like this: 我会做这样的事情:

#include <iostream>
#include <map>
#include <string>

int main()
{
    std::string st;

    std::cout << "Input a sentence: " << std::endl;
    std::getline(std::cin, st);

    std::map<char, int> m;

    for (const char c : st)
        ++m[c];

    for (const std::pair<char, int>& me : m)
        std::cout << me.first << ": " << me.second << std::endl;
}

lechuga2000 beat me to the post, but this is my suggestion: lechuga2000击败了我,但我的建议是:

#include <iostream>
#include <map>
#include <string>

int main()
{
    std::string input_sentence = "Now is the time for all good men to come to the aid of the party.";

/*
    std::cout << "Input a sentence: " << std::endl;
    std::getline(std::cin, input_sentence);
*/

    std::map<char, int> character_counts;

    for (const auto character : input_sentence)
        ++character_counts[character];

    for (const auto counted_character : character_counts)
        std::cout << counted_character.first << ": " << counted_character.second << '\n';

    return 0;
}

And here is the output: 这是输出:

 : 15
.: 1
N: 1
a: 3
c: 1
d: 2
e: 6
f: 2
g: 1
h: 3
i: 3
l: 2
m: 3
n: 1
o: 8
p: 1
r: 2
s: 1
t: 7
w: 1
y: 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM