K&R Exercise 1-9 (C)

后端 未结 30 1102
北荒
北荒 2021-01-31 19:46

\"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 thi

相关标签:
30条回答
  • 2021-01-31 20:18

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-31 20:19

    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);
        }
    }
    
    0 讨论(0)
  • 2021-01-31 20:19
    #include <stdio.h>
    
    main() {
        int input, last = EOF;
        while ((input = getchar()) != EOF) {
           if (input == ' ' && last == ' ') continue;
           last = input; 
           putchar(input);
        }
    }
    
    0 讨论(0)
  • 2021-01-31 20:19
    #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;
    }
    
    0 讨论(0)
  • 2021-01-31 20:20

    Look at your program as a machine that moves between different states as it iterates over the input.

    It reads the input one character at a time. If it sees anything other than a blank, it just prints the character it sees. If it sees a blank, it shifts to a different state. In that state, it prints one blank, and then doesn't print blanks if it sees them. Then, it continues reading the input, but ignores all blanks it sees--until it hits a character that isn't a blank, at which point it shifts back to the first state.

    (This concept is called a finite state machine, by the way, and a lot of theoretical computer science work has gone into what they can and can't do. Wikipedia can tell you more, though in perhaps more complicated detail than you're looking for. ;))

    0 讨论(0)
  • 2021-01-31 20:20
    // K & R Exercise 1.9
    // hoping to do this with as few lines as possible 
    
    int c = 0, lastchar = 0;
    c = getchar();
    while (c != EOF) {
      if (lastchar != ' ' || c != ' ') putchar(c);
      lastchar=c;
      c=getchar();
    }
    
    0 讨论(0)
提交回复
热议问题