gpt4 book ai didi

c++ - 麻烦数词

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:26:13 26 4
gpt4 key购买 nike

<分区>

我遇到了关于字数统计程序的问题。我希望程序能够告诉我给定字符串中有多少个单词、行、字符、唯一行和唯一单词。但是,我不断收到有关此的问题。有人可以帮助我,特别是关于空白字符吗?

#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;

}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com