Yet another K&R exercise that I am having some troubles with. Heh.... I was just accepted for a series of night school computer courses at the local community college, so hopefully I will get on to some of this stuff better and more quickly.
But as for the trouble I have..
The exercise is to:
"Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank"
After some consideration of how the solution might work, this is what I came up with:
The idea was to count the blanks, and if the variable 'blank' got higher than 1, to set the value of c to one blank, then putchar() to the output - This 'solution' of mine definitely does not work the way I want it to, and is not close to the answers posted here , however, perhaps somebody would be willing to give me a hint, and point me in the right direction. I am aware that I have possibly done some poor logical structure, but I will leave the critique to all of you. :)Code:
#include <stdio.h>
/* This is from K&R and what it is supposed to do is *
* copy the input to the output, and then replace *
* each string of one or more blanks with a single *
* blank. *
*/
int main(void)
{
int c;
int done;
int blank;
done = 0;
blank = 0;
while ( done == 0 )
{
c = getchar();
if ( c == ' ' )
++blank;
{
if ( blank >= 2 )
( c = ' ' );
putchar( c );
blank = 0;
}
c = getchar();
if ( c == EOF )
done = 1;
}
return 0;
}
kermit