如何计算出现在字符串和输出行中的唯一单词?C
How to count unique words in string and output line where they occur? C++
,所以我正在从事此功课,我真的很麻烦。我应该计算两个以上字符的单词数(必须包含一个字母),唯一的单词以及每个唯一单词在编程执行环境中出现的次数。我还应该在小便中获取输入,并输出出现的次数以及它出现的线路。我有一些工作,但是我确实在努力计算每个单词出现多少次。我知道我的代码现在真的很糟糕,但这就是为什么我在这里。出于某种原因,我真的很努力这些字符串功能。任何帮助都非常感谢!
#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]))
,我建议您使用特定器''分开字符串,然后做一些事情。
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- 何时在引用或唯一指针上使用移动语义
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 我需要编写一个程序来读取一个文件,该文件将输出所有唯一的整数,如果已经看到整数,它将被关闭
- C :如何计算数组中唯一客户端的数量并输出其支出
- 为什么两个相同的代码给出不同的输出,而它们之间的唯一区别是不同的变量名称和写作样式
- 如何计算出现在字符串和输出行中的唯一单词?C
- 输出对象中唯一字符的数量
- 输出 4 组 7 个升序有序和唯一随机数
- 我想从特定的c++数组中输出唯一的项
- 在布尔矩阵中查找唯一的行(帮助解析std::string的垃圾输出)
- 输出“std::multiset”的唯一元素及其频率,使用C++中的std::算法(无循环)