I've started studying "The ANSI C Programming Language" by Dennis Ritchie and Brian W.Kernighan. So far I've just learned getchar(),putchar(),while, for, if. There is an exercise that I have to do only using what I've learned by now. The above are the only things I know. Here's the exercise as it appears in the book:
Exercise 1-9. Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.
I know C#, Pascal, Objective-C and some Java but I cannot understand how this problem can be solved without using arrays. I know arrays but as the author has not covered arrays yet I assume I must not use them. Arrays are taught in the next chapters.
Print the first whitespace (blank) character you see, skip the rest. That means having a boolean variable that is set to
falseas soon you see a non-whitespace character, and set totruewhen you hit and print first whitespace. Do not print if that variable istrue. Kind of a two-state "state machine".