K&R Exercise 1-9 (C)

后端 未结 30 1105
北荒
北荒 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:28

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

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

    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;
        }
    
    }
    
    0 讨论(0)
  • 2021-01-31 20:31
    #include <stdio.h>   
    main()
    {
        int CurrentChar, LastChar;
        LastChar = '1';
        while ((CurrentChar = getchar()) != EOF)
        {
            if (CurrentChar != ' ')
            {
                putchar(CurrentChar);
                LastChar = '1';
            }
            else
            {
                if (LastChar != ' ')
                {
                    putchar(CurrentChar);               
                    LastChar = ' ';
                }                   
            }   
        }
    }
    
    0 讨论(0)
  • 2021-01-31 20:32

    I am also starting out with the K&R textbook, and I came up with a solution that uses only the material which had been covered up until that point.

    How it works:

    First, set some counter 'blanks' to zero. This is used for counting blanks.

    If a blank is found, increase the counter 'blanks' by one.

    If a blank is not found, then first do a sub-test: is the counter 'blanks' equal or bigger than 1? If yes, then first, print a blank and after that, set the counter 'blanks' back to zero.

    After this subtest is done, go back and putchar whatever character was not found to be a blank.

    The idea is, before putcharing a non-blank character, first do a test to see, if some blank(s) were counted before. If there were blanks before, print a single blank first and then reset the counter of blanks. That way, the counter is zero again for the next round of blank(s). If the first character on the line is not a blank, the counter couldn't have increased, hence no blank is printed.

    One warning, I haven't gone very far into the book, so I'm not familiar with the syntax yet, so it's possible that the {} braces might be written in different places, but my example is working fine.

    #include <stdio.h>
    
    /* Copy input to output, replacing each string of one or more blanks by a single blank. */
    
    main()
    {
        int c, blanks;
    
        blanks = 0;
        while ((c = getchar()) != EOF) {
            if (c != ' ') {
                if (blanks >= 1)
                    printf(" ");
                    blanks = 0;
                putchar(c); }
            if (c == ' ')
                ++blanks;
        }
    }
    
    0 讨论(0)
  • 2021-01-31 20:32
    #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.

    0 讨论(0)
提交回复
热议问题