The code testing whether ch == ' ' is outside the if statement which detects whether there is input available, so it will be processing the value of ch each time through this section of code, not just once for each received character. If this code is executing within loop() then the value of space is probably incrementing very rapidly and overflowing quite frequently - the value will be effectively random. Just move that second if statement inside the braces for the first one, and it should do what you want.
Incidentally, if your code was correctly indented the mistake would have been far more obvious. Tools / Autoformat makes this very easy to do and I recommend that you do it.