Re: Problem with printing input.
- From: Barry Schwarz <schwarzb@xxxxxxxxx>
- Date: Wed, 20 Apr 2005 18:04:39 -0700
On 20 Apr 2005 15:01:57 -0700, "Matt" <noXealia@xxxxxxxxx> wrote:
>Ok, so the exercise is to:
>
>Write a program that prints its input one word per line.
>
>Does this mean so that when I prest ^Z(ctrl-z) to exit the program if I
>were to do this:
>
>Hey you
>
>It would do this:
>
>Hey
>you
>
>? If so then I am really lost on howto do that, I've tried putchar, but
>that doesn't work(mainly cause well it prints it right after I say it,
>and only the 1st letter). I'm really sorry for bothering so much, but I
>tried to solve this myself before asking, I tried, I failed. Could
>someone please help me? The original code(before printing the words on
>new lines(just telling how many words)) is this:
>
>#include <stdio.h>
>
>/* print the input one word per time on different lines */
>
>#define IN 1
>#define OUT 0
>
>int main(void)
>{
> int c, state;
> char nw;
>
> state = OUT;
You should limit the use of state to only indicate if you have output
any letters (to prevent skipping multiple lines if there is more than
one space between two words).
> nw = 0;
> while ((c = getchar()) != EOF) {
> if (c == ' ' || c == '\n' || c == '\t')
You might want to consider using the isspace function here.
> state = OUT;
You also need to print out the character. Consider using putchar.
> else if (state == OUT) {
> state = IN;
> ++nw;
You did not mention any requirement to count the number of words. Why
are you counting in a char instead of an int.
You need to output a \n here to complete this word's line.
> }
> }
> printf("%d\n", nw);
Don't you think a title to go with the number would be nice?
> return 0;
>}
<<Remove the del for email>>
.
- References:
- Problem with printing input.
- From: Matt
- Problem with printing input.
- Prev by Date: Re: C99 Restrict
- Next by Date: Re: Problem with printing input.
- Previous by thread: Problem with printing input.
- Next by thread: Re: Problem with printing input.
- Index(es):
Relevant Pages
|