Why do I always get run time error on "hello world!"


  • 1
    Z

    My approach is simply push the index first char of each word into a stack. Then pop them back one by one, once reach ' ' or '\0', pop the next word. Adding space after each word.

    If there is at least one word exists, delete the last char. Since I added space after all the word. Space after the last word is not necessary.

    I always get the run time error with this input "hello world!"

    here is the code, please help me on this, It works fine on my machine with output "world! hello". And I already asked this question for three times no one actually gave me the answer. Thank you!

        std::vector<int> stack;
        bool wordFlag=true;
        for(int ii=0;ii<s.size();ii++){
            if(s[ii]==' ')
                wordFlag=true;
            if((((s[ii]>=97)&&(s[ii]<=122))||
                ((s[ii]>=65)&&(s[ii]<=90)))&&
               (wordFlag==true)){
                stack.push_back(ii);
                wordFlag=false;
            }
        }
        char* tempString=new char;
        int wordIndex;
        int tempStringIndex=0;
        while(stack.begin()!=stack.end()){
            wordIndex=stack.back();
            stack.pop_back();
            while((s[wordIndex]!=' ')&&(s[wordIndex]!='\0')){
                tempString[tempStringIndex++]=s[wordIndex++];
            }
            tempString[tempStringIndex++]=32;
        }
        s.assign(tempString,strlen(tempString));
        if(s.size())
            s.resize(s.size()-1);
        }

  • 0
    A

    I got the same error. I guess when the input is "hello world!", the right output may be "world hello!" rather than "world! hello". And I will try it. Touch me if you have any ideas :)


  • 0
    Z

    Hi, I figured out the problem. Run Time Error is like segmentation fault. So I was thinking maybe I try to reach somewhere out of the address space.

    If you could look at my code above. After pushing the first letters into the stack. I create a char pointer in the heap. In my laptop, I can create a char array in this way.Whereas it seems that it won't be allowed here. I haven't delve into very detail why this is happening yet. But after I changed my code like this

    std::string sTemp;
    int wordIndex;
    while(!stack.empty()){
        wordIndex=stack.back();
        stack.pop_back();
        while((s[wordIndex]!=' ')&&(s[wordIndex]!='\0')){
            sTemp.push_back(s[wordIndex++]);
        }
        sTemp.push_back(' ');
    }
    s.assign(sTemp);
    if(s.size())
        s.resize(s.size()-1);
    }
    

    It is accepted.


  • 0
    A

    i guess,the statement:' char* tempString=new char;",i think you only get a byte(size of char) on heap,and get a size of pointer(maybe 4 bytes or 8 bytes,due to the system) space on stack.when you use"tempString[tempStringIndex++]=s[wordIndex++];",it is out of the address space that you have,it overwrite the other space,and the c/c++ does not detect out of bound error,but it does not result in run-time error immediately in your computer


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.