Re: HELP!! Two questions from <<the c programming language>>



Tak <kakataka@xxxxxxxxx> writes:

Algorithm trim_space

<bad algorithm snipped>

My program of exercise 1-9

#include <stdio.h>

int main()
{
int flag=1;
int c;

while ((c = getchar()) != EOF)
{
if (c == ' ')
{
flag = 0;
}
else if (flag == 0)
{
printf(" ");
putchar(c);
flag = 1;
}
else
putchar(c);
}
}

It seems run OK,without error or warning in Vc++6.0.
Does it still contain any problems?

Yes. What happens when the input ends with one or more spaces?

--
Ben.
.



Relevant Pages

  • Re: Discarding unread data after scanf()
    ... You need to test for EOF. ... stream, so there's no risk of discarding any more than needed. ... getchar() returns EOF on failure or end of file. ... int skip2nl ...
    (comp.lang.c)
  • Re: Whats the deal with the "toupper" family?
    ... plus EOF which is guaranteed not to be in that range. ... Not according to C89. ... According to C89, getchar() is equivilent ... returning "an unsigned char converted to an int". ...
    (comp.lang.c)
  • Re: C newbie problem
    ... while(c = getchar()!= EOF){ ... Your avoidance of typing has led you to write a program without defined behavior under either the old or new standard. ...
    (comp.lang.c)
  • Re: Why getchar() doesnt quit if EOF isnt the first char
    ... Why getchar() doesn't quit if EOF isn't the first char] ... int main ... It's a value returned by getcharto indicate an end-of-file condition; that value is distinct from any character value. ...
    (comp.lang.c)
  • Re: C newbie problem
    ... while(c = getchar()!= EOF){ ... Before C99, mainwould implicitly be declared int, but then you would need something equivalent to 'return 0;': ...
    (comp.lang.c)