J
Josh Zenker
I've written a solution based on Richard Heathfield's
(http://users.powernet.co.uk/eton/kandr2/krx112.html), but I'm
encountering some unexpected program behavior. Here's my code:
#include <stdio.h>
#define IN 1 /* inside a space */
#define OUT 0 /* outside a space */
/* prints input one word per line */
int main() {
int c, state;
state = OUT;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t') {
if (state == OUT) {
state = IN;
putchar('\n');
}
/* else print nothing */
} else {
state = OUT;
putchar(c);
}
}
return 0;
}
I compiled with gcc-3.4.6 on Gentoo. When I run the executable, it
works fine for inputs like "foo bar" but gets fouled up when the input
contains a newline. For example, I typed "foo" followed by Ctrl+V and
Enter, followed by "bar" (i.e. "foo^Mbar") and got only "bar" as the
output. What's going on here?
JZ
(http://users.powernet.co.uk/eton/kandr2/krx112.html), but I'm
encountering some unexpected program behavior. Here's my code:
#include <stdio.h>
#define IN 1 /* inside a space */
#define OUT 0 /* outside a space */
/* prints input one word per line */
int main() {
int c, state;
state = OUT;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t') {
if (state == OUT) {
state = IN;
putchar('\n');
}
/* else print nothing */
} else {
state = OUT;
putchar(c);
}
}
return 0;
}
I compiled with gcc-3.4.6 on Gentoo. When I run the executable, it
works fine for inputs like "foo bar" but gets fouled up when the input
contains a newline. For example, I typed "foo" followed by Ctrl+V and
Enter, followed by "bar" (i.e. "foo^Mbar") and got only "bar" as the
output. What's going on here?
JZ