如何计算出现在字符串和输出行中的唯一单词?C

How to count unique words in string and output line where they occur? C++

本文关键字:输出 唯一 单词 字符串 何计算 计算      更新时间:2023-10-16

,所以我正在从事此功课,我真的很麻烦。我应该计算两个以上字符的单词数(必须包含一个字母),唯一的单词以及每个唯一单词在编程执行环境中出现的次数。我还应该在小便中获取输入,并输出出现的次数以及它出现的线路。我有一些工作,但是我确实在努力计算每个单词出现多少次。我知道我的代码现在真的很糟糕,但这就是为什么我在这里。出于某种原因,我真的很努力这些字符串功能。任何帮助都非常感谢!

#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
//PEE string
string envstr("");
bool checkChar(unsigned c)
{
    return (ispunct(c) || isspace(c) || isblank(c) || isdigit(c) || c == 'n');
}
void searchWord(unsigned c, size_t length)
{
    multiset<string> words;
    vector<string> vwrds; //this was something i was trying out
    string tempword;
    while (!checkChar(envstr[c]) && c < length)
    {
        tempword = tempword + envstr[c]; //problem here
        c++;
    }
    tempword = tempword + " ";
    vwrds.push_back(tempword); 
    words.insert(tempword); //this is just a bunch of random letters
    tempword.clear();
    //for (multiset<string>::const_iterator i(words.begin()), end(words.end()); i != end; i++)
        //cout << *i;
}
bool checkIfWord(char c)
{
    bool valid = false;
    int i;
        for (i = c; i > c - 2; i--)
        {
            if (!checkChar(envstr[i]))
                valid = true;
        }
        if (valid)
            searchWord(i, envstr.length());
    return valid;
}
int main()
{
    //this code given by my instructor
    extern char **environ; // needed to access your execution environment
    int k = 0;
    size_t wordCount = 0;
    while (environ[k] != NULL)
    {
        cout << environ[k] << endl;     
        string str(environ[k]);
        envstr = envstr + str;
        k++;
    }
    //iterator to count words
    wordCount = count_if(envstr.begin(), envstr.end(), checkIfWord);
    cout << "nThe PEE contains " << wordCount << " words. n";
    //transform environment string to lowercase
    transform(envstr.begin(), envstr.end(), envstr.begin(), tolower);
    string input;
    do
    {
        cout << "Enter your search item: n";
        cin >> input;
        //string can only be forty characters
        if (input.length() > 40 || input == "n")
        {
            cout << "That search query is too long. n";
            continue;
        }
        //change the search string to lowercase, like the envstr
        transform(input.begin(), input.end(), input.begin(), tolower);
        int j = 0;
        int searchCount = 0;
        vector<size_t> positions;
        size_t pos = envstr.find(input, 0);
        //search for that string
        while (pos != string::npos)
        {
            positions.push_back(pos);
            pos = envstr.find(input, pos + 1);
            searchCount++;
        }
        cout << "nThat phrase occurs a total of " << searchCount << " times.n";
        cout << "It occurs in the following lines: n";
        //output where that string occurs
        for (vector<size_t>::iterator it = positions.begin(); it != positions.end(); ++it)
        {
            for (int i = *it; i < envstr.length() - 1 && checkChar(envstr[i]); i++)
            {
                cout << envstr[i];
            }
            cout << endl;
        }
        positions.clear();
    } while (input != "END");
    cin.get();
    return 0;
}

首先,您的函数checkChar()在参数为char时返回false,因此,如果要打印该字符串的位置,则应该是:

for (int i = *it; (i < envstr.length() - 1) && !checkChar(envstr[i]); i++)
{
    cout << envstr[i];
}

第二,计数单词的代码没有任何意义,这里有潜在的局限性: if (!checkChar(envstr[i])),我建议您使用特定器''分开字符串,然后做一些事情。

<</p>