Re: Problem with printing input.



try this ...

#include <stdio.h>

/* print the input one word per time on different lines */

int main(void)
{
int c,space_flag=0;

while ((c = getchar()) != EOF)
{
if ((c == ' ' || c == '\t'))
space_flag++;
else if (space_flag != 0)
{
putchar('\n');
putchar(c);
space_flag = 0;
}
else
putchar(c);
}

return 0;
}




Matt 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;
> nw = 0;
> while ((c = getchar()) != EOF) {
> if (c == ' ' || c == '\n' || c == '\t')
> state = OUT;
> else if (state == OUT) {
> state = IN;
> ++nw;
> }
> }
> printf("%d\n", nw);
> return 0;
> }

.



Relevant Pages

  • Re: calendar problem
    ... >> Hey people ... >> I came across this calendar problem on another board, ... >> int main ... > in simple cases like this, just output variables' values at ...
    (comp.lang.c)
  • Problem with printing input.
    ... so the exercise is to: ... Does this mean so that when I prest ^Zto exit the program if I ... Hey you ... int main ...
    (comp.lang.c)
  • Re: Problem with printing input.
    ... >Hey you ... >int main ... Consider using putchar. ... are you counting in a char instead of an int. ...
    (comp.lang.c)
  • Re: string formatter %x and a class instance with __int__ cannot handle long
    ... Are you hoping it will cast it to an int as needed using ... #!/usr/bin/env python ... print "Hey! ...
    (comp.lang.python)
  • Re: Fibonacci program
    ... >i have to program a cobol program where the prompt lists the fibonacci ... hey try this formula: ... int main ... "What a waste it is to lose one's mind. ...
    (comp.lang.cobol)