By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,591 Members | 650 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,591 IT Pros & Developers. It's quick & easy.

K&R2, Exercise 1-12

P: n/a
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

Sep 21 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Josh Zenker schrieb:
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 */
If you just want different values, enumeration constants IMO are
a better choice.
>
/* prints input one word per line */
int main()
int main (void)
is the recommended form around here.
{
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?
Are you sure that this input gives you what you expect?
Change your output to
printf("\\n[%d]\n", '\n');
and
printf("%c[%d]\n", c, c);
respectively to see what is going on.

If you can, redirect the input from a file to test your
programme -- then it should work as expected.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 21 '06 #2

P: n/a
Josh Zenker wrote:
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?
In ASCII, ^M is a carriage return. ^J is newline.

Probably your terminal treats it as such, i.e. it returns the cursor
to the start of the line but does not go down a line.

So your program outputs foo, and then goes back to the start of
the same line, and overwrites foo with bar.

Try typing "foot^Mbar", you will probably see "bart".

Sep 21 '06 #3

P: n/a
Old Wolf wrote:
Josh Zenker wrote:
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?

In ASCII, ^M is a carriage return. ^J is newline.

Probably your terminal treats it as such, i.e. it returns the cursor
to the start of the line but does not go down a line.

So your program outputs foo, and then goes back to the start of
the same line, and overwrites foo with bar.

Try typing "foot^Mbar", you will probably see "bart".
Yes , this must be it. To be absolutely certain you
can pipe the output of the programme to od.

A more advanced version of your programme may
contain some special logic to handle control characters.

Sep 22 '06 #4

P: n/a
Old Wolf wrote:
Josh Zenker wrote:
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?

In ASCII, ^M is a carriage return. ^J is newline.

Probably your terminal treats it as such, i.e. it returns the cursor
to the start of the line but does not go down a line.

So your program outputs foo, and then goes back to the start of
the same line, and overwrites foo with bar.

Try typing "foot^Mbar", you will probably see "bart".
Yes, you're absolutely right. Thanks for clearing that up.

JZ

Sep 25 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.