簡體   English   中英

C ++中的字符計數器?

[英]Character Counter in C++?

我試圖找到一種方法來顯示字符串中的所有字符以及它們出現的次數。

這是我到目前為止的內容:

//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;
}

我返回這個:

輸入一個句子:

你好

小時:1

e:1

l:2

l:2

o:1

所以我有一些東西,但是我不知道如何使字母不重復一次。 我知道我需要將它們存儲在一個數組中,但這已經超出了我的范圍。

您非常親密,不錯的嘗試! 我喜歡使用counters數組的方法,其中每個單元格都代表給定字符串中字母的頻率。

因此,只需更新此數組,因為此答案暗示着如何獲得C語言中字母在字符中的位置? ,因為那里需要數組中字母的索引,所以沒有在這里提到的加號。 換句話說,對於“ a”,您需要0,“ b”,您需要1,依此類推。

然后,在打印階段,以相反的方式使用以上鏈接的建議。 當您打印counters的第i個非零元素時,請打印該元素的第i個元素,這將顯示有問題的字母。

放在一起,您將獲得:

#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;
}

輸出:

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

我會做這樣的事情:

#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擊敗了我,但我的建議是:

#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;
}

這是輸出:

 : 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