如何从字符串中删除重复的单词并只用他们的字数显示一次

How do I remove repeated words from a string and only show it once with their wordcount

基本上,我必须显示每个单词及其计数,但重复的单词会再次出现在我的程序中。

如何使用循环删除它们,或者我应该使用二维数组来存储单词和计数?

#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <cstring>
#include <conio.h>
#include <time.h>
using namespace std;

char* getstring();
void xyz(char*);
void tokenizing(char*);

int main()
{
    char* pa = getstring();
    xyz(pa);
    tokenizing(pa);

    _getch();
}

char* getstring()
{
    static char pa[100];
    cout << "Enter a paragraph: " << endl;
    cin.getline(pa, 1000, '#');

    return pa;
}
void xyz(char* pa)
{
    cout << pa << endl;
}
void tokenizing(char* pa)
{
    char sepa[] = " ,.\n\t";
    char* token;
    char* nexttoken;
    int size = strlen(pa);
    token = strtok_s(pa, sepa, &nexttoken);
    while (token != NULL) {
        int wordcount = 0;
        if (token != NULL) {
            int sizex = strlen(token);
            //char** fin;
            int j;
            for (int i = 0; i <= size; i++) {
                for (j = 0; j < sizex; j++) {
                    if (pa[i + j] != token[j]) {
                        break;
                    }
                }
                if (j == sizex) {
                    wordcount++;
                }
            }
            //for (int w = 0; w < size; w++)
            //fin[w] =  token;
            //cout << fin[w];

            cout << token;
            cout << " " << wordcount << "\n";
        }
        token = strtok_s(NULL, sepa, &nexttoken);
    }
}

这是我得到的输出:

我想显示,例如,单词 "i" 计数为 5,然后不再显示。

我读了你最后的评论。

但是很抱歉,我不会C。所以,我会用C++来回答。

但无论如何,我会用 C++ 标准方法来回答。那通常只有 10 行代码。 . .

#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <regex>

// Regex Helpers
// Regex to find a word
static const std::regex reWord{ R"(\w+)" };
// Result of search for one word in the string
static std::smatch smWord;

int main() {
    std::cout << "\nPlease enter text: \n";
    if (std::string line; std::getline(std::cin, line)) {

        // Words and its appearance count
        std::map<std::string, int> words{};

        // Count the words
        for (std::string s{ line }; std::regex_search(s, smWord, reWord); s = smWord.suffix())
            words[smWord[0]]++;

        // Show result
        for (const auto& [word, count] : words) std::cout << word << "\t\t--> " << count << '\n';
    }
    return 0;
}

首先,由于您使用的是 C++,我建议您使用 C++ 方式拆分文本(一些示例是 here), and store every word in map or unordered_map. Example of my realization you can find here

但是如果您不想重写您的代码,您可以简单地添加一个变量来指示是在单词位置之前还是之后找到该单词的副本。如果前面没有找到副本,则打印你的话

post 给出了一个示例,将 'strtok' 函数中的每个单词保存到字符串向量中。然后,使用 string.compare 将每个单词与 word[0] 进行比较。那些与 word[0] 匹配的索引被标记在一个 int 数组 'used' 中。匹配计数等于使用的数组中的数字标记 ('nused')。然后将标记的那些词从向量中移除,剩下的进行下一个比较过程。当没有留下任何文字时,程序结束。

如果你不想用std::vector和std::string,你可以写一个单词比较函数来代替'str.compare(str2)'。

#include <iostream>
#include <string>
#include <vector>
#include<iomanip>
#include<cstring>
 using namespace std;
      
 char* getstring();
 void xyz(char*);
 void tokenizing(char*);
 
 int main()
 {
    char* pa = getstring();
    xyz(pa);
    tokenizing(pa);
 }

 
char* getstring()
{
   static char pa[100] = "this is a test and is a test and is test.";
   return pa;
}
void xyz(char* pa)
{
  cout << pa << endl;
}
void tokenizing(char* pa)
{
   char sepa[] = " ,.\n\t";
   char* token;
   char* nexttoken;
   std::vector<std::string> word;
   int used[64];
   std::string tok;
   int nword = 0, nsize, nused;
   int size = strlen(pa);
   token = strtok_s(pa, sepa, &nexttoken);
   while (token)
   {
      word.push_back(token);
      ++nword;
      token = strtok_s(NULL, sepa, &nexttoken);
   }
   for (int i = 0; i<nword; i++) std::cout << word[i] << std::endl;
   std::cout << "total " << nword << " words.\n" << std::endl;
   nsize = nword;
   while (nsize > 0)
   {
       nused = 0;
       tok = word[0] ;
       used[nused++] = 0;
       for (int i=1; i<nsize; i++)
       {
           if ( tok.compare(word[i]) == 0 )
           {
              used[nused++] = i; }
       }
       std::cout  << tok << " : " << nused << std::endl;
       for (int i=nused-1; i>=0; --i)
       {
          for (int j=used[i]; j<(nsize+i-nused); j++) word[j] = word[j+1];
       }
       nsize -= nused;
   }
}

请注意,使用过的单词的删除必须按倒序进行。如果按顺序执行,则需要更改 'used' 数组中标记的索引。 运行 测试:

$ ./a.out
this is a test and is a test and is test.
this
is
a
test
and
is
a
test
and
is
test
total 11 words.

this : 1
is : 3
a : 2
test : 3
and : 2