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 08:53

This is what I got:

while ch = getchar()
   if ch != ' '
      putchar(ch)
   if ch == ' '
      if last_seen_ch != ch
         putchar(ch)
   last_seen_ch = ch
查看更多
Emotional °昔
3楼-- · 2020-05-17 08:54

Pseudo code

while c = getchar:
    if c is blank:
        c = getchar until c is not blank
        print blank
    print c

C

You can substitute use of isblank here if you desire. It is unspecified what characters contrive blank, or what blank value is to be printed in place of others.

After many points made by Matthew in the comments below, this version, and the one containing isblank are the same.

int c;
while ((c = getchar()) != EOF) {
    if (c == ' ') {
        while ((c = getchar()) == ' ');
        putchar(' ');
        if (c == EOF) break;
    }
    putchar(c);
}
查看更多
叛逆
4楼-- · 2020-05-17 08:55

I wrote this and seems to be working.

 # include <stdio.h>
 int main ()
{

int c,lastc;
lastc=0;
while ((c=getchar()) != EOF)
    if (((c==' ')+ (lastc==' '))<2)
        putchar(c), lastc=c;
 }
查看更多
Lonely孤独者°
5楼-- · 2020-05-17 08:55

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;
    }
}       

}

查看更多
一纸荒年 Trace。
6楼-- · 2020-05-17 08:57
1.Count the number of blanks.
2.Replace the counted number of blanks by a single one.
3.Print the characters one by one.

<code>
main()
{
    int c, count;
    count = 0;
    while ((c = getchar()) != EOF)
    {
        if (c == ' ')
        {
            count++;
            if (count > 1) 
            {
                putchar ('\b');
                putchar (' ');
            }
            else putchar (' ');
        }
        else 
        {
            putchar (c);
            count = 0;
        }
    }
    return;
}
</code>
查看更多
混吃等死
7楼-- · 2020-05-17 08:58
#include <stdio.h>
int main(void)
{
        long c;
        long nb = 0;
        while((c = getchar()) != EOF) {
                if(c == ' ' || c == '\t') {
                        ++nb;
                } else {
                        if(nb > 0) {
                                putchar(' ');
                                nb = 0;
                        }

                        putchar(c);
                }
        }
        return 0;
}
查看更多
登录 后发表回答