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:16

First declare two variables character and last_character as integers.when you have not reach the end of the file( while(character=getchar() != EOF ) do this; 1. If character != ' ' then print character last_character = character 2. If character == ' ' if last_character ==' ' last character = character else print character

查看更多
劫难
3楼-- · 2020-05-17 09:16
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;
 }
查看更多
我命由我不由天
4楼-- · 2020-05-17 09:17

Since relational operators in C produce integer values 1 or 0 (as explained earlier in the book), the logical expression "current character non-blank or previous character non-blank" can be simulated with integer arithmetic resulting in a shorter (if somewhat cryptic) code:

int c, p = EOF;
while ((c = getchar()) != EOF) {
    if ((c != ' ') + (p != ' ') > 0) putchar(c);
    p = c;
}

Variable p is initialized with EOF so that it has a valid non-blank value during the very first comparison.

查看更多
闹够了就滚
5楼-- · 2020-05-17 09:17

a way to make it easier for the new people are stuck on this book (by not knowing any thing then what brought up until page 22 in K&R).

credits to @Michael , @Mat and @Matthew to help me to understand

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

while ((c = getchar()) != EOF) /* state of "an input is not EOF" (1) */ 
 {
        if (c == ' ') /* "blank has found" -> change the rule now */
        {
          while ((c = getchar ()) == ' '); /* as long you see blanks just print for it a blank until rule is broken (2) */
          putchar(' ');
        }
   putchar(c); /* either state (2) was broken or in state (1) no blanks has been found */
  }
}
查看更多
放荡不羁爱自由
6楼-- · 2020-05-17 09:17
#include<stdio.h>
#include<stdlib.h>

int main(void)
{
 int c, flag=0;

   while((c=getchar()) != EOF){

      if(c == ' '){
         if(flag == 0){
            flag=1;
            putchar(c);
        }
    }else{
        flag=0;
        putchar(c);
     }

   }

  return 0;

}

I hope this will help.

查看更多
登录 后发表回答