【问题标题】:Creating a function the returns frequency of given words in C++在 C++ 中创建一个函数返回给定单词的频率
【发布时间】:2019-11-25 20:58:23
【问题描述】:

好的,所以我不确定是什么导致我收到错误

分段错误(核心转储)

但我每次运行程序时都会得到它。如果有人可以解释我做错了什么,那就太好了。

#include <iostream>
#include <string>
#include <string.h>

using namespace std;

void frequency(char* paragraph, string words[],int num_words){
    //Declare the required variables.
    string temp = "";
    int freq[num_words];
    //Start the loop to set the frequency of each word to 0.
    for(int i=0;i<num_words;i++){
        freq[i] = 0;
    }
    //Start the loop to traverse the paragraph.
    for(int i=0;i<=strlen(paragraph);i++){
        //Check the string if a space or null is found.
        if(paragraph[i] == ' ' || paragraph[i] == '\0'){
            //Start the loop to compare each word.
            for(int j=0;j<num_words;j++){
                int flag = 0;
                //Start the loop to comapare the word.
                for(int k=0;k<words[j].length();k++){
                    //Convert both the characters to lowercase to compare ignoring the case.
                    if(tolower(temp[k]) != tolower(words[j].at(k))){
                        //Break the loop if the character does not match.
                        flag = 1;
                        break;
                    }
                }
                //Increase the frequency if the word matches with the current string.
                if(flag == 0)
                    freq[j]++;
            }
            //Reset the string.
            temp = " ";
        }
        //Otherwise, append the character in the string.
        else{
            temp = temp+paragraph[i];
        }
    }
    //Start the loop to print the frequency of each word.
    for(int i=0;i<num_words;i++){
        cout<<words[i]<<": "<<freq[i]<<endl;
    }
}

int main()
{
    char *paragraph;
    string *words;
    string swords;
    cout << "Enter the paragraph: ";
    cin.getline(paragraph, 250);
    cout << "Enter the words to search: ";
    getline(cin, swords);
    int spaces = 0;
    //Start the loop to count the number of words.
    for(int i = 0; i < swords.length(); i++){
        if(swords.at(i) == ' ')
            spaces++;
    }
    int num_words = spaces + 1;
    int pos = 0;
    string temp = " ";
    //Create an array of strings to store the words.
    words = (string*) new string[num_words];
    //Start the loop to store the words in the array.
    for(int i = 0; i < swords.length(); i++)
    {
        //Store the string in the array if a space is encountered.
        if(swords.at(i) == ' ')
        {
            words[pos] = temp;
            temp = " ";
            ++pos;
        }
        //Otherwise, append the character in the string.
        else
        {
            temp += swords.at(i);
        }
    }
    //Store the last string in the array.
    words[pos] = temp;
    //Call the function to compute and display the frequency.
    frequency(paragraph, words, num_words);
    //Return 0 and exit the code.
    return 0;
}

我知道分段错误的发生主要是因为您试图访问您无权访问的内存(通常通过将变量设置为 null 或类似的东西)但我终生无法找到我自己的代码中的错误。如果你能提供任何帮助,请做

【问题讨论】:

  • 你能提供你提供给程序的输入,以及你在seg错误之前得到的所有输出吗?
  • cin.getline(paragraph, 250); - 段落没有指向任何地方,您使用的是未初始化的指针。为什么指定 paragraph 可以容纳 250 个字符?
  • 仅供参考,要获得 C++ 中单词的频率计数大约需要 5 行代码——如果您愿意编写 C++ 而不是 C 并进行一些语法更改。
  • 既然已经在使用 std::string,为什么还要使用 char 数组和 char 指针?
  • 好的,我将如何用 5 行代码编写这段代码?我是一名全新的计算机科学专业学生,我只想学习。我正在使用 char 数组,因为这是分配所需要的

标签: c++ string substring


【解决方案1】:

编译你的代码:

g++ -g -fsanitize=address main.cpp

然后运行它。

然后程序将在导致分段错误的行上退出,并告诉您它试图访问导致问题的内存。

【讨论】:

    【解决方案2】:

    您应该在循环中使用 '

        for(int i=0;i<= strlen(paragraph);i++) part
    ...
    

    【讨论】:

    • 至少请指出在for循环条件中使用strlen()的严重缺陷。
    • 太痛苦了!但如果 OP 很幸运,它可能会得到优化。
    • 假设段落有 250 个字符。如果 OP 不走运,那就是 62,500 次循环迭代。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 2011-07-03
    • 1970-01-01
    • 2021-06-22
    相关资源
    最近更新 更多