C ++,计算字符串中的重复单词并显示

时间:2012-08-06 18:32:53

标签: c++ arrays string

我有字符串说“走路说话,不会不会不会说话”。我想数一下翻新的单词和显示。 注意:它不区分大小写。

我使用了分隔符

strtok(string, ",.;:\"!? -_\n\t*()@#=+");

并将其保存在

char *temp[100];

现在我如何检查单词的重复?并显示如下

3 won't
2 talk
1 can't
1 walk

它应显示从最高重复到最低。如果重复相同则显示字母顺序。

抱歉我的英语不好。

4 个答案:

答案 0 :(得分:3)

使用std :: string来保存strtok()的结果。然后创建一个std::map<string, int>来保存字符串(密钥)发生的次数。

您可以使用以下内容填充地图:

std::map<string, int> myMap;
myMap[tokenizedWord]++; //Increase count of word.

然后,您可以循环浏览地图内容,并在整数值大于2的任何地方打印出来。

for (std::map<string, int>::iterator iter = myMap.begin(); iter != myMap.end(); ++iter)
{
    if (iter->second > 1)
        std::cout << "Duplicated word: " << iter->first << " count = " << iter->second;
}

我会让你弄清楚如何按顺序遍历它。您可以将值放在矢量或其他内容中,然后在打印之前使用std::sort或其他任何您喜欢的内容。不幸的是,地图是关联容器,你不能对它们进行排序,因为它会破坏它们的内部排序。

std :: map的背景信息

映射是一个关联数组,意味着每个键都映射到特定值,并且键是唯一的。实际上,您可以创建一个多重映射,其中键不是唯一的,因此这很重要。

基本上,由于密钥是唯一的,因此只需将密钥用作数组索引即可访问或创建元素。

例如:

//Create a map and insert a couple things into it - prices of meat?
std::map<string, float> myMap;
myMap["Chicken"] = 4.99;
myMap["Turkey"] = 6.99;

//Retrieve the price of something using the key.
std::cout << "Chicken costs " << myMap["Chicken"] << std::end;

您也可以在地图上执行标准插入和定位操作,但关联数组语法更简单,为什么还要烦恼呢? :)

PS:要完全回答您的评论,以防万一,myMap [tokenizedWord] ++末尾的++只是说增加为该键存储的整数值的值你可以做myMap [tokenizedWord] = myMap [tokenizedWord] + 1或者你也可以做myMap [tokenizedWord] + = 1。

答案 1 :(得分:1)

完整实现您的问题(如果您想要一个示例代码进行排序,请告诉我们):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#define ARRAY_ELEMS_COUNT(A)    sizeof(A)/sizeof(*A)

typedef struct _word_t
{
        char    *word;
        int     occurr_count;
        struct _word_t  *next;
} word_t;

typedef struct _word_list_t
{
        struct  _word_t *head;
        struct  _word_t *tail;
        int     elems_count;
} word_list_t;

/* Creation of the words list */
word_list_t *make_list(void)
{
        word_list_t *w_list = (word_list_t *)malloc(sizeof (struct _word_list_t));
        if (w_list == NULL)
        {
                fprintf(stderr, "malloc faild --> %s\n", strerror(errno));

                return NULL;
        }
        w_list->head = w_list->tail = NULL;
        w_list->elems_count = 0;

        return w_list;
}

int list_word_lookup(word_list_t *w_list, char *word)
{
        word_t *temp_word = w_list->head;
        while(temp_word)
        {
                if (strcmp(temp_word->word, word) == 0)
                {
                        /* We got it before, increment the count */
                        temp_word->occurr_count++;

                        return 1;
                }
                else
                {
                        temp_word = temp_word->next;
                }
        }

        return 0;
}

/* Adding new words to the list of words if they are not present, otherwise increment their occurrence count */
/* TODO : Sort the list using Merge sort for performance */
int adding_to_list(word_list_t *w_list, char *word)
{
        int     return_status = 0;
        char    *tmp_word = (char *)malloc(sizeof(char)*(strlen(word) + 1));
        word_t  *new_word = (word_t *)malloc(sizeof(struct _word_t));
        /* Empty list */
        if (w_list->head == NULL)
        {
                strcpy(tmp_word, word);
                new_word->word = tmp_word;
                new_word->occurr_count = 1;
                w_list->head = w_list->tail = new_word;
                w_list->head->next = NULL;
                w_list->elems_count++;
        }
        else
        {
                /* The list is not empty */
                /* Checking if the word exist in the list */
                return_status = list_word_lookup(w_list, word);
                if (return_status == 1)
                {
                        fprintf(stdout, "WE got this word before --> increment count\n");
                }
                else
                {
                        strcpy(tmp_word, word);
                        new_word->word = tmp_word;
                        new_word->occurr_count = 1;
                        w_list->tail->next = new_word;
                        w_list->tail = new_word;
                        w_list->tail->next = NULL;
                }
        }

        return 0;
}

void words_list_dump(word_list_t *w_list)
{
        word_t *temp;

        for (temp = w_list->head; temp; temp = temp->next) {
                fprintf(stdout, "Word : %s -- Count = %d\n", temp->word, temp->occurr_count);
        }
}

/* Destroying all words */
void free_words(word_list_t *w_list)
{
        word_t *temp;

        for (temp = w_list->head; temp; temp = temp->next) {
                /* Freeing the word string */
                free(temp->word);
                /* Freeing the word */
                free(temp);
        }
        w_list->head = NULL;
        w_list->tail = NULL;
}

/* Destroying the words list */
void free_words_list(word_list_t *w_list)
{
        if (!w_list)
        {
                return;
        }
        free_words(w_list);
        free(w_list);
}

/* TODO : create a function that converts your input text to a char ** array, so you can pass it to adding_to_list */
/* For testing */
int main(int argc, char **argv)
{
        const char *string[] = {"Hello", "World", "Stackoverflow", "C", "Hello", "C", "WORDS", "words", "List", "list", "Hello", "World", "Count"};
        word_list_t *my_list = make_list();
        int i;

        for (i = 0; i < ARRAY_ELEMS_COUNT(string); i++)
                adding_to_list(my_list, string[i]);
        words_list_dump(my_list);
        free_words_list(my_list);

        return 0;
}

答案 2 :(得分:0)

以下是使用strtok但没有std::map的答案。在一个字符串传递中,每个单词都与前一个单词进行检查,并重复计算。

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <string>
using std::string;

#include <vector>
using std::vector;

#include <cstring>

using std::tolower;

int main()
{
    char *strin;
    string inputstr;
    vector<string> svec;
    vector<int> cvec;
    char *pch;
    int unique_word_count=0;
    while(getline(cin,inputstr))
    {
        //token-ize the string
        //First string
        strin = &inputstr[0];
        pch = std::strtok(strin," ,-");
        bool unique_word_found = true;
        //subsequent words
        while (pch != NULL)
        {
            string word(pch);
            for(string::size_type i=0; i < word.size(); i++)
                word[i]=tolower(word[i]);
            //first word
            //just add to svec and no comparisons
            if(unique_word_count==0)
            {
                svec.push_back(word);
                cvec.push_back(1);
                cvec[unique_word_count++]=1; //init count of first word
                //next word
                pch = std::strtok(NULL, " ,-");
                unique_word_found = true; //reset flag
                continue;
            }

            //start comparing with other words currently in string vector
            //do not do this if only 1 word present
            vector<string>::iterator iter=svec.begin();
            while(iter < svec.end())
            {
                if(word == *iter)
                {
                    //match found
                    cvec[iter-svec.begin()]++; //increment count of that word
                    unique_word_found = false;
                }
                iter++;
            }
            if(unique_word_found)
            {
                //add to unique word list and increment count
                svec.push_back(word);
                cvec.push_back(1);
                cvec[unique_word_count++]=1;
            }

            //next word
            pch = std::strtok(NULL, " ,-");
            unique_word_found = true; //reset flag
        }
    }

    cout << "Word" << " ---> " << "Occurences" << endl;
    for(vector<string>::size_type i=0; i < svec.size(); i++)
    {
        cout << svec[i] << "  --->  " << cvec[i] << endl;
    }
    return 0;
}

答案 3 :(得分:0)

总体策略如下:

  • 清理输入(将所有字符转换为小写,删除不需要的标点符号等)
  • 浏览输入
  • 将每个字符添加到字符串中,在遇到空格时最终确定
  • 将字符串添加到键值结构中。字符串是关键。如果这是一个尚未包含在结构中的新条目,则设置值1.否则将其设置为当前值+ 1(以便计算到目前为止遇到的次数)。
  • 对每个单词重复
  • 浏览键值结构并打印每个条目。