我遇到了关于字数统计程序的问题。我希望程序能够告诉我给定字符串中有多少个单词、行、字符、唯一行和唯一单词。但是,我不断收到有关此的问题。有人可以帮我特别是关于空白字符吗?
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <set>
using std::set;
unsigned long countLines(const string& s)
{
int nl = 0;
for(int x =0; x<s.size(); x++)
{
if(s[x] == "\0")
nl++;
}
return nl;
}
unsigned long countWords(const string& s)
{
int nw = 0;
char ws = " ";
for (int x = 0; x<s.size(); x++)
{
if (s[x] == ws)
{
nw++;
}
}
return nw;
}
unsigned long countChars(const string& s)
{
int nc = 0;
for (int x = 0; x < s.size(); x++)
{
if ( s[x] != " ")
{
nc++;
}
}
return nc++;
}
unsigned long countuline(const string& s, set<string>& wl)
{
wl.insert(s);
return wl.size(s);
}
unsigned long countuwords(const string& s, set<string>& wl)
{
int nuw = 0;
char ws = " ";
wl.insert(s);
for (int x = 0; x<s.size(); x++)
{
if (s[x] == ws)
{
nuw++;
}
}
return nuw;
}
int main()
{
string line;
while (getline(cin,line))
{
cout << countLines(line) << "\t" << countWords(line) << "\t" << countChars(line) << endl;
}
return 0;
}