K&R Exercise 1-9 (C)

Viewed 14900

"Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank."

I'm assuming by this he means input something like...

We(blank)(blank)(blank)go(blank)to(blank)(blank)(blank)the(blank)mall!

... and output it like:

We(blank)go(blank)to(blank)the(blank)mall!

This is probably easier than I'm making it out to be, but still, I can't seem to figure it out. I don't really want the code... more so pseudo code.

Also, how should I be looking at this? I'm pretty sure whatever program I write is going to need at least one variable, a while loop, a couple if statements, and will use both the getchar() and putchar() functions... but besides that I'm at a loss. I don't really have a programmers train of thought yet, so if you could give me some advice as to how I should be looking at "problems" in general that'd be awesome.

(And please don't bring up else, I haven't got that far in the book so right now that's out of my scope.)

31 Answers
#include <stdio.h>
main()
{
    int c, numBlank=0 ;
    while((c= getchar())!=EOF)
    {
        if(c ==' ')
    {
        numBlank ++;
        if(numBlank <2)
        {
        printf("character is:%c\n",c);
        }
    }
    else
    {
        printf("character is:%c\n",c);
        numBlank =0;
    }
    }
}

Using the constraints of not using else or and operators. This code only prints a blank when the blank variable is equal to 1 and the only way to reset the counter is by typing something other than a blank. Hope this helps:

include

/* Write a program that replaces strings of blanks with a single blank */

void main(){ int c, bl;

bl = 0;

while((c = getchar()) != EOF){
    if(c == ' '){
        ++bl;
        if(bl == 1){
            putchar(' ');
        }
    }
    if(c != ' '){
        putchar(c);
        bl = 0;
    }
}       

}

Many others have already used the last character logic in their code, but perhaps the following version is easier to read:

int c, prevchar;
while ((c = getchar()) != EOF) {
    if (!(c == ' ' && prevchar == ' ')) {
        putchar(c);
        prevchar = c;
    }
}
for(nb = 0; (c = getchar()) != EOF;)
{
    if(c == ' ')
       nb++;
    if( nb == 0 || nb == 1 )
       putchar(c);
    if(c != ' '  &&  nb >1)
       putchar(c);
    if(c != ' ')
       nb = 0;
 }

Considering what's asked in the question, I have also made sure that the program runs smooth in case of various tabs, spaces as well as when they're clubbed together to form various combinations! Here's my code,

int c, flag = 1;
    printf("Enter the character!\n");
    while ((c = getchar()) != EOF) {
    if (c == ' '||c == '\t') {
        c=getchar();
        while(c == ' '|| c == '\t')
               {
                   c = getchar();
               }
        putchar(' ');
        if (c == EOF) break;
    }
    putchar(c);
}

Feel free to run all test cases using various combinations of spaces and tabs.

Solution1: as per topics covered in the k&R book:

#include <stdio.h>

int main()
{
    int c;

while ((c = getchar()) != EOF)
    {
       if  (c == ' ') 
       {while ( getchar() == ' ' )
        ;  // ... we take no action
        }

        putchar(c);

    }

return 0;
}

Solution2 : using program states:

int main()
{
    int c, nblanks = 0 ;

while ((c = getchar()) != EOF)  
{
   if (c != ' ')
       { putchar(c);
         nblanks = 0;}

   else if (c==' ' && nblanks == 0) // change of state
            {putchar(c);
             nblanks++;}
}
return 0;
}

Solution3 : based on last seen char

int main()
{
int c, lastc = 0;

while ((c = getchar()) != EOF)
    {
        if ( c != ' ')
        {putchar(c);}


        if (c == ' ')
        {
            if (c==lastc)
                ;
            else putchar(c);

        }

        lastc = c;
    }

return 0;
}

This is how I approached it;

make input loop
check if the character is \t; if it is, then change the value of that character to four blanks
check if the character is a blank
make a while loop that runs as long as the next character (another getchar) is a blank
if the statement above is still running, set a state variable to 1
after the loop; check if the state is 1; if it is, then set the current character to a single blank
print the remaining inputs

Scroll down for code















Code:

/* Exercise 1-9 */
#include <stdio.h>

/* Program to replace 2 or more blanks in input with a single blank */
int main() {
    int c;
    int blank_state;

    while ((c = getchar()) != EOF) {
        blank_state = 0;
        if (c == '\t')
            c = '    ';
        if (c == ' ') {
            while((c = getchar()) == ' ')
                blank_state = 1;
        }

        if (blank_state == 1)
            c = ' ';
        putchar(c);
    }
}
Related