K&R Exercise 1-9 (C)

2020-05-17 08:26发布

"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.)

标签: c
29条回答
冷血范
2楼-- · 2020-05-17 09:13

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;
    }
}
查看更多
家丑人穷心不美
3楼-- · 2020-05-17 09:13

Like many other people, I am studying this book as well and found this question very interesting.

I have come up with a piece of code that only uses what has been explained before the exercice (as I am not consulting any other resource but just playing with the code).

There is a while loop to parse the text and one if to compare the current character to the previous one. Are there any edge cases where this code would not work ?

#include <stdio.h>

main() {

    // c    current character
    // pc   previous character
    int c, pc;

    while ((c = getchar()) != EOF) {
        // A truthy evaluation implies 1 
        // (learned from chapter 1, exercice 6)
        // Avoid writing a space when 
        //  - the previous character is a space (+1)
        //  AND
        //  - the current character is a space (+1)
        // All the other combinations return an int < 2
        if ((pc == ' ') + (pc == c) < 2) {
            putchar(c);
        }
        // update previous character
        pc = c;
    }

}
查看更多
ゆ 、 Hurt°
4楼-- · 2020-05-17 09:14

Here is how I think of the algorithm of this exercise, in pseudo-code:

define ch and bl (where bl is initially defined to be 0)

while ch is = to getchar() which is not = to end of file  
do the following:  
      if ch is = to blank and bl is = 0  
          --> output ch and assign the value 1 to bl  
      else --> if ch is = to blank and bl is = 1  
          --> do nothing  
      else --> output ch and assign the value 0 to bl

Example implementation in C:

#include <stdio.h>
#include <stdlib.h>

main() {

   long ch,bl=0;

   while ((ch=getchar()) != EOF)
   {
       if (ch == ' ' && bl == 0)
       {
           putchar(ch);
           bl=1;
       } else if (ch == ' ' && bl == 1) {
           // no-op
       } else {
           putchar(ch);
           bl=0;
       }
   }

   return 0;
}
查看更多
\"骚年 ilove
5楼-- · 2020-05-17 09:15

I worked really hard at finding a solution that used only the material that has already been covered in the first part of the first chapter of the book. Here is my result:

#include <stdio.h>

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

main()
{
    int c;

    while ((c = getchar()) != EOF){
        if (c == ' '){
            putchar(c);
            while ((c = getchar()) == ' ')
                ;
        }
        if(c != ' ')
            putchar(c);
    }
}
查看更多
Lonely孤独者°
6楼-- · 2020-05-17 09:15

I am at the same point in the book. and my solution goes with making a count++ if blank is found and making the count back to zero if anything other than blank is found.

For if statement I put another another check to check value of count (if zero) and then print.

Though at this point of learning I shouldn't be concern about efficiency of two methods but which one is efficient a.) Accepted solution here with while inside while or b.) the one I suggested above.

My code goes like below:

#include <stdio.h>


main()
{
    int count=0,c;
    for( ; (c=getchar())!=EOF; )
    {
        if(c==' ')
        {
            if(count==0)
            {
                putchar(c);
                count++;
            }
        }
        if(c!=' ')
        {
            putchar(c);
            count=0;
        }


    }
}
查看更多
我只想做你的唯一
7楼-- · 2020-05-17 09:15

To do this using only while loops and if statements, the trick is to add a variable which remembers the previous character.

Loop, reading one character at a time, until EOF:
    If the current character IS NOT a space:
        Output current character

    If the current character IS a space:
        If the previous character WAS NOT a space:
            Output a space

    Set previous character to current character

In C code:

#include <stdio.h>

main()
{
    int c, p;

    p = EOF;

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

        if (c == ' ')
            if (p != ' ')
                putchar(' ');

        p = c;
    }
}
查看更多
登录 后发表回答