2

wordcounting プログラムに関する問題が発生しました。指定された文字列に含まれる単語、行、文字、一意の行、および一意の単語の数をプログラムに教えてもらいたいです。しかし、私はこれに関して問題を抱え続けています。特に空白文字に関して誰かが私を助けてくれますか?

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

}
4

2 に答える 2

0

countLines は常に 1 を返し、文字列 s は使用されていません。nl を常にゼロに設定し、戻る前に 1 ずつ増やします。

    int nl = 0;

countWords、この文 "Hello World!" の結果は?

于 2013-09-25T18:06:49.087 に答える