trying to count space from serial input

I was tying to count the number of spaces of an input from the serial monitor.
Ex: I type “I like airplanes” then my Space variable would = 2

But I am getting weird numbers. Part of my code is below.

if (Serial.available())
   ch =;
   StringOut += ch; 
  if (ch == ' '){
   space += 1;

Any help?

What type of variable is ch ? Have you tried printing ch after reading it ?
Posting your your whole program would have helped.

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.