Re: Problem with printing input.
- From: ramakrishnat@xxxxxxxxx
- Date: 21 Apr 2005 04:54:55 -0700
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;
> }
.
- References:
- Problem with printing input.
- From: Matt
- Problem with printing input.
- Prev by Date: Re: newbie having trouble with conversion program
- Next by Date: Re: finding size of directory
- Previous by thread: Re: Problem with printing input.
- Next by thread: What all gcc does
- Index(es):
Relevant Pages
|