Article 57DZQ Exercise 1-12 in the K&R - Problem with stdout.

Exercise 1-12 in the K&R - Problem with stdout.

by
Arct1c_f0x
from LinuxQuestions.org on (#57DZQ)
"Exercise 1-12. Write a program that prints its input one word per line."

I'm trying to figure out what stupidly obvious thing I've overlooked, lend me a hand?

Quote:
#include <stdio.h>

// This program will print its input one word per line

#define IN 1 //
#define OUT 0 //

int main() {

int c, word, state;

state = OUT;

while ((c = getchar()) != EOF) {

if (c != ' ' || c != '\t' || c != '\n') {
state = IN;
word += c;
}
if (c == ' ' || c == '\t' || c == '\n') {
state = OUT;
printf("%d\n", word);
word = 0;
}

}
}
Here's what I came up with but my program would only print numbers instead of printing single words. Earlier in the book it says that you can delcare an int variable and then use it to store strings as long as the numbers within the variable are interpreted back into characters before being displayed to StdOut. Did it really say that or am I dellusional? I can't find it now. What did I do wrong? I tried replacing the printf() statement with put char but then I ended up with even stranger output! everytime I would type an 'h' it would display an 'r' to stdout, and everytime I would type a random letter it would print some other random letter different from the one that I typed. I'm going to keep trying to figure out what I did wrong but I'm going to check back to see what you guys have to say periodically.

My humble gratitude,latest?d=yIl2AUoC8zA latest?i=YUhwrPAukTI:-30z9TgXH7o:F7zBnMy latest?i=YUhwrPAukTI:-30z9TgXH7o:V_sGLiP latest?d=qj6IDK7rITs latest?i=YUhwrPAukTI:-30z9TgXH7o:gIN9vFwYUhwrPAukTI
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments