How do we deal with punctuations like '!' or '.'


  • 0
    Z

    They have a case "Hello World!"

    Shall the result be "World! Hello" or "World Hello!"

    The requirement says A sequence of non-space characters constitutes a word. Well since ! is also a char.

    Here is the code, just in case.

    I received a runtime error like:

    Runtime Error
    Last executed input: "hello world!"

            vector<char*> myStack;
            int stackTopIndex=0;
            bool pushable=true;
            for(int ii=0;ii<s.size();ii++){
                if(s[ii]==' ')
                    pushable=true;
                else if((s[ii]!=' ')&&(pushable==true)){
                    pushable=false;
                    myStack.push_back(&s[ii]);
                    stackTopIndex++;
                }
            }
            //pull them out in the reverse order
            char* newS=new char;
            int indexNewS=0;
            for(int ii=stackTopIndex-1;ii>=0;ii--){
                char* tempChar;
                tempChar=myStack[ii];
                while((*tempChar!='\0')&&(*tempChar!=' ')){
                    newS[indexNewS]=*tempChar;
                    //cout<<"_"<<newS[indexNewS]<<endl;
                    tempChar++;
                    indexNewS++;
                }
                if(ii!=0){
                    newS[indexNewS]=' ';
                    indexNewS++;
                }else{
                    newS[indexNewS]='\0';
                }
                
            }
            s.assign(newS);

  • 0
    M

    i don't think there are any test cases where there is punctuation, but '!' is not a space(' '), so it would be part of the word it's next to. In your example, it would be "World! Hello".

    EDIT: As you've added the runtime error question to the punctuation one, here's my runtime error answer:

    First off, this is something that would be better as a separate question. By asking in an answered question, you reduced the number of people who will see and answer, and prevent people from getting the normal amount of points. Normally, they would get the points for the punctuation question and points for the runtime error, but now they only get one. Another disadvantage is that this makes it very difficult to find the question for other people. Only the people who enter this question will ever see the question, thinking it is about the punctuation problem, so may ask the same question when it's already been answered. Now, on to your question.

    A runtime error is something like a segfault, or an indexoutofboundsexception, where it is compilable code, but crashes in mid-execution. Because it doesn't finish running, nor runs out of time, the tester does not give what the expected answer was. It gives you what input you crashed on, which means you can take that input and run it on your own machine to try and debug what caused that crash using prints and breakpoints. If you remain stuck after trying that, take your code and use a question to ask everyone, showing them the input, your code, and what runtime error it was (incredibly useful in helping to debug the problem).


  • 1
    G

    I guess there are no text cases in this question . However In general ,When I was asked this question in the interview I had to ask the interviewer about all the assumptions.

    I was told to consider special char's as not special chars.

    => My answer would be "World! Hello" according to the specifications.


  • 0
    Z

    Hi, I know this is weird. But I got a Runtime Error like this...
    Submission Result: Runtime Error

    Last executed input: "hello world!"

    Could you please tell me what does it mean? Since it doesn't say "Expected: "something""Thank you!


  • 0
    Z

    Hi, I know this is weird. But I got a Runtime Error like this... Submission Result: Runtime Error

    Last executed input: "hello world!"

    Could you please tell me what does it mean? Since it doesn't say "Expected: "something""Thank you!


  • 0
    M
    This post is deleted!

Log in to reply
 

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