接收分段错误与程序分割句子 [英] Receiving segmentation fault with a program to split sentences

查看:123
本文介绍了接收分段错误与程序分割句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题指针,当我运行我的程序我收到分割falut(核心倾销)任何人可以帮助我知道哪个部分出现此错误?

I have problem with pointers when I run my programe I recieve a segmentation falut (core dumped) can anyone help me know which part made this error?

这是一个用于通过获取分割符来分割句子的程序

This is a program for splitting sentences by getting a splitter I used two dementioned array

#include <iostream>
using namespace std;


int main(int argc, char **argv)
{
    char Sentence[255];
    cin.getline(Sentence,255);
    char splitter='_';
    int lenOfStr=0;
    int numOfWords=1;
    while (Sentence[lenOfStr]!='\0'){
        if (Sentence[lenOfStr]==splitter) numOfWords++;
        lenOfStr++;
    }

    char** words=new char* [numOfWords];
    int* lenOfEachWord=new int[numOfWords];
    int lenth=0,wordNum=0;
    for (int i=0;i<lenOfStr;i++){
        if (Sentence[i]==splitter) {
            lenOfEachWord[wordNum]=lenth;
            lenth=0;
            wordNum++;
        }
        else lenth++;

    }
    for (int word=0;word<numOfWords;word++){
        int lenth=lenOfEachWord[word];
        words[word]=new char[lenth];
    }
    int word=0;
    int chr=0;
    while (chr<lenOfStr){
        for (int i=0 ;i<lenOfEachWord[word];i++){
            words[word][i]=Sentence[chr];
            chr++;
        }
        word++;
    }
    for (int i=0;i<numOfWords;i++){
        for (int j=0;j<lenOfEachWord[i];j++){
            cout<<words[i][j];
        }
        cout<<endl;
    }
    for (int i=0;i<numOfWords;i++){
        delete[] words[i];
    }
    delete[] words;


    return 0;
}


推荐答案

为您的代码。我试着用这个代码测试,我发现有一些边界,你错过了注意,像你错过计数最后一个字,并没有在复制过程中排除分离器。但我不确定你确定词数是否是正确的方式,我认为样本输入应该只有两个词。

you should try to add log for your code. I try to test with this code, and I find there is some boundary you missed paying attention to ,like you miss to count the last word,and didn't exclude the splitter during the copy process. but I am not sure the way you determine the number of words is whether is right or not, I think the sample input should have only two words.

但我会离开它为你决定。希望它有帮助

but I will leave it for you to decide. hope it helps

输入:_test_string _

input : _test_string_

输出:

numOfWords: 4
lenOfStr: 13
0 4 6 0 
0
1
1 0 1 t
1 1 2 e
1 2 3 s
1 3 4 t
2
2 0 6 s
2 1 7 t
2 2 8 r
2 3 9 i
2 4 10 n
2 5 11 g

test
string

#include <iostream>
#include <cstring>
using namespace std;



int main(int argc, char **argv)
{
    char Sentence[255];
    cin.getline(Sentence,255);
    char splitter='_';
    int lenOfStr=0;
    int numOfWords=1;
    while (Sentence[lenOfStr]!='\0'){
        if (Sentence[lenOfStr]==splitter) numOfWords++;
        lenOfStr++;
    }
    std::cout <<"numOfWords: " <<  numOfWords <<std::endl;
     std::cout <<"lenOfStr: " <<  lenOfStr <<std::endl;
    char** words=new char* [numOfWords];
    int* lenOfEachWord=new int[numOfWords];

    int lenth=0,wordNum=0;
    for (int i=0;i<lenOfStr;i++){
        if (Sentence[i]==splitter) {
            lenOfEachWord[wordNum]=lenth;
            lenth=0;
            wordNum++;
        }
        else lenth++;
    }
    // be care with the boundary 
    if (lenth >= 0)
    {
        lenOfEachWord[wordNum]=lenth;
        wordNum++;
    }
    for(int i =0 ; i< numOfWords; i++)
    {
        std::cout << lenOfEachWord[i] << " ";
    }
    std::cout<<std::endl;
    for (int word=0;word<numOfWords;word++){
        int lenth=lenOfEachWord[word];
        words[word]=new char[lenth];
    }
    int word=0;
    int chr=0;
    while (chr<lenOfStr){
        if (Sentence[chr] == splitter )
        {
            chr++;
            continue;
        }
        std::cout << word << endl;
        for (int i=0 ;i<lenOfEachWord[word];i++){
            words[word][i]=Sentence[chr];
            std::cout << word << " " << i << " " << chr<< " "<< Sentence[chr]<<endl;
            chr++;
        }
        word++;
    }
    for (int i=0;i<numOfWords;i++){
        for (int j=0;j<lenOfEachWord[i];j++){
            cout<<words[i][j];
        }
        cout<<endl;
    }
    for (int i=0;i<numOfWords;i++){
        delete[] words[i];
    }
    delete[] words;


    return 0;
}

这篇关于接收分段错误与程序分割句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆