【发布时间】:2014-04-21 15:45:17
【问题描述】:
所以我对此非常陌生。我有一个任务来计算用户输入的行数、单词数、字符数、唯一行数和唯一单词数。到目前为止,我已经从我的代码中获得了线条、独特的线条和字符。我以为我明白了,但是当我考虑到双空格和制表符时,它就不起作用了。我也不知道如何找到独特的词。请提供您的帮助。
代码:
// What I dont have:
//words
//Total words
#include <iostream>
#include <string>
#include <set>
using namespace std;
unsigned long countWords(const string& s, set<string>& wl); //total words
int main()
{
int linenum=0, charnum=0, totalwords=0;
set<string> lines;
string input;
set<string> unique; //to store unique words from countWords function
while (getline(cin,input))
{
lines.insert(input);
linenum++;
charnum+= input.length();
totalwords += countWords(input,unique);
}
cout << linenum <<" "<< totalwords <<" "<< charnum <<" " << lines.size()<<" " << unique.size()<< endl;
system("PAUSE");
return 0;
}
unsigned long countWords(const string& s, set<string>& wl) //total words
{
int wcount=1;
for (unsigned int i=0; i < s.length(); i++)
{
if ((s.at(i) == ' ')&&(s.at(i)+1 !='\0')) {
wcount++;
}
}
return wcount;
}
【问题讨论】:
-
@Suyog 类似,但增加了查找唯一词的要求
标签: c++ string set substring word-count