繁体   English   中英

C++ 需要帮助计算函数中的字数。 (例如,Hello World = 2)

[英]C++ need help figuring out word count in function. (Ex. Hello World = 2)

我正在弄清楚这个函数的算法,它在运行时不断崩溃,这是代码片段:

int wordCounter(char usStr[]) {
    int index= 0, punct= 0;

    while(usStr[index]!= '\0') //If it's not the end of the sentence
        if(usStr[index]== ' ') //If it finds a space
            index++;

    while(usStr[index]== '\0') //If it's the end of the sentence.
        punct++;
    int allChar= punct+ index;

    return allChar;
}

如果需要,我会发布完整的程序,但现在我需要有人帮助我解决问题的根源。

更新:这是我的 int main。 假设 numWords 是一个函数,它接受一个字符串类对象作为其参数并要求用户输入:

int main()
{
string userVal;
    numWords(userVal);
    char *conStr= new char[' ']; //I'm very doubtful and worried about the contents insing the [].
    strcpy(conStr, userVal.c_str()); //String converted to a C-string.
    int fin= wordCounter(conStr);
    cout<< "The number of words in the sentence is "<< fin<< "."<< endl;
    pause();
    return 0;
}

虽然我不知道你真正要求的是什么,但我认为你应该有一个条件来结束你的第一个while()循环以在单词的开头进行调整:

int wordCounter(char usStr[]) {
    int index= 0, punct= 0;

    while(usStr[index]!= '\0') {  // <<< Use effin' braces
        if(usStr[index]== ' ') { // <<< Use effin' braces
            index++;
        }
        else {
            break;
        }
    }

    // index points to a non ' ' character or '\0' here
    // No idea, what you're trying to achieve with the following lines
    // of code. 

    while(usStr[index] == '\0') { // If it's the end of the sentence ...
                                  // ... this part loops forever!
        punct++;
    }

    int allChar = punct + index;

    return allChar;
} 
                int wordCounter(char usStr[]) {
        int index= sizeof(usStr);
        int result = 0;

        for (int i=0; i<index; i++){
            if(usStr[index]== ' ')
                   result++;
            }//end of the loop

        return result+1;

        } //try this one.

简单的数组方式:

int wordCounter(char usStr[]) 
{
    int wordcount = 0;
    int charcount = 0;
    int index = 0;
    while (usStr[index] != '\0')
    { 
        if (usStr[index] == ' ')
        {
            if (charcount)
            { //only count non-empty tokens
                wordcount++;
                charcount = 0;
            }
        }
        else
        {
            charcount++;
        }
        index++;
    }
    if (charcount)
    {  // get last word, if any.
         wordcount++;
    }
    return wordcount;
}

简单的 C++ 方式:

int wordCounter(char usStr[]) 
{
    int wordcount = 0;
    std::stringstream stream(usStr);
    std::string temp;
    while (stream >> temp)
    { // got a token
        wordcount++;
    }
    return wordcount;
}

故障原因分解:

while(usStr[index]!= '\0') //good
    if(usStr[index]== ' ') //good
        index++; //bad

让我们看一个简单的案例“嗨!”

迭代 1: index = 0, usStr[index] = H

while(usStr[index]!= '\0')

H != '\\0',输入

    if(usStr[index]== ' ') //good

H != ' ',不要输入。 不增加索引

迭代 2:index = 0,usStr[index] = H

while(usStr[index]!= '\0')

H != '\\0',输入

    if(usStr[index]== ' ') //good

H != ' ',不要输入。 不增加索引

迭代 3:index = 0,usStr[index] = H

while(usStr[index]!= '\0')

H != '\\0',输入

    if(usStr[index]== ' ') //good

H != ' ',不要输入。 不增加索引

看到问题了吗? 如果没有,我可以彻夜穿越。 不过,还是吃点东西睡觉吧。

请注意,我是初学者。 对不起,我觉得我不得不说,以防我完全错了。 :) 希望我不必说太久。

无论如何,我会以我能想到的最简单的方式做到这一点。 我会使用 std::string 而不是 char 数组。 这是我制作的一个小函数,它源自我制作的将 std::strings 拆分为子字符串的函数。

// split the string s into substrings and count the number of substrings
int count_substrings(const string& s)
{
    int count{ 0 };
    stringstream ss{ s };
    for (string buffer; ss >> buffer;)
        ++count;
    return count;
}

所以它的作用是获取字符串并将其存储到字符串流中。 然后“ss >> buffer”将遍历字符串中的字符,当它遇到空格时,它将将该子字符串存储到“buffer”中并进入for循环,这会增加“count”。 每次找到子字符串时,它都会进入 for 循环,这就是它计算子字符串数量的方式。 请注意,“缓冲区”实际上并未在任何地方使用。 如果这是对正在发生的事情的错误解释,我很抱歉,但我正在尝试。 :) 我希望这可以帮助您了解我在做什么。 请,如果我错了,请纠正我。

然后对这个函数的一个非常简单的测试可能是这样的:

cout << "Enter a string:\n";
string str;
getline(cin, str);

int count = count_substrings(str);
cout << "Number of substrings == " << count << '\n';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM