tags:

views:

194

answers:

3

"Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank."

I'm assuming by this he means input something like...

We(blank)(blank)(blank)go(blank)to(blank)(blank)(blank)the(blank)mall!

... and output it like:

We(blank)go(blank)to(blank)the(blank)mall!

This is probably easier than I'm making it out to be, but still, I can't seem to figure it out. I don't really want the code... more so pseudo code.

Also, how should I be looking at this? I'm pretty sure whatever program I write is going to need at least one variable, a while loop, a couple if statements, and will use both the getchar() and putchar() functions... but besides that I'm at a loss. I don't really have a programmers train of thought yet, so if you could give me some advice as to how I should be looking at "problems" in general that'd be awesome.

(And please don't bring up else, I haven't got that far in the book so right now that's out of my scope.)

+4  A: 

Pseudo code

while c = getchar:
    if c is blank:
        c = getchar until c is not blank
        print blank
    print c

C

You can substitute use of isblank here if you desire. It is unspecified what characters contrive blank, or what blank value is to be printed in place of others.

After many points made by Matthew in the comments below, this version, and the one containing isblank are the same.

int c;
while ((c = getchar()) != EOF) {
    if (c == ' ') {
        while ((c = getchar()) == ' ');
        putchar(' ');
        if (c == EOF) break;
    }
    putchar(c);
}
Matt Joiner
Just ask if you'd like this in C syntax.
Matt Joiner
One thing that might help: this might be a good time to learn about `ctype.h` if you don't know about it yet. `ctype.h` contains functions for determining the "type" of a `char`. That is, you can use it to see if a `char` is a letter, a number, whitespace, etc.If it's too much to absorb now, just make a mental note for later so you don't waste time re-inventing the wheel.
michael
It will output a garbage character (EOF) if the last character is blank.
Matthew Flaschen
@Matthew: Thanks! Good catch!
Matt Joiner
@michael, not sure if that's aimed at me or OP, i tried to keep it simple as they're new to C
Matt Joiner
Ah, I didn't know you could put a 'while' inside a 'while' ... and could you repost the first version (the simpler one without else) too. Don't delete the new one, just have them both on their so I can compare because for now I haven't learned 'else'.And what is 'isblank'?
MW2000
Sure, will do..
Matt Joiner
Awesome, thanks!
MW2000
In the "space is blank" version, the `c != EOF` in the inner `while` is redundant.
Matthew Flaschen
@Matthew: This time you are wrong :). Keep in mind that this version is highly simplified, and that EOF can be returned in later calls to getchar() in the same loop. Feel free to rip up any redundancies in the last version however.
Matt Joiner
Matthew Flaschen
Oh yeah, again, good catch. I've been in python land for 8 months.
Matt Joiner
+13  A: 

Look at your program as a machine that moves between different states as it iterates over the input.

It reads the input one character at a time. If it sees anything other than a blank, it just prints the character it sees. If it sees a blank, it shifts to a different state. In that state, it prints one blank, and then doesn't print blanks if it sees them. Then, it continues reading the input, but ignores all blanks it sees--until it hits a character that isn't a blank, at which point it shifts back to the first state.

(This concept is called a finite state machine, by the way, and a lot of theoretical computer science work has gone into what they can and can't do. Wikipedia can tell you more, though in perhaps more complicated detail than you're looking for. ;))

Michael Louis Thaler
Great explanation Michael.
MW2000
@MW2000: this is how regular expressions matchers work. They build a specific finite state machine from the pattern you specify. If you don't know what a regex is just check it out: http://en.wikipedia.org/wiki/Regular_expression
Jack
A: 

http://users.powernet.co.uk/eton/kandr2/index.html has an answer to this question, too: http://users.powernet.co.uk/eton/kandr2/krx109.html

The above site may be useful for later excercises, too.

Lars Wirzenius
Bookmarked, thanks. I'm sure I'll be tempted to look at it a lot :).
MW2000
That site is no longer updated. http://clc-wiki.net/wiki/K%26R2_solutions is the new location.
schot