K&R Exercise 1-9 (C)

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

    Here is my answer, I am currently in the same spot you were years ago.

    I used only the syntax taught until this point in the books and it reduces the multiple spaces into one space only as required.

    #include<stdio.h>
    int main(){
        int c
        int blanks = 0; // spaces counter
        while ((c = getchar()) != EOF) {        
            if (c == ' ') { // if the character is a blank
                while((c = getchar()) == ' ') { //check the next char and count blanks
    
                    blanks++;
    
                    // if(c == EOF){
                    // break;
                    // }
                }            
                if (blanks >= 0) { // comparing to zero to accommodate the single space case, 
                                   // otherwise ut removed the single space between chars
                    putchar(' '); // print single space in all cases                    
                }
    
            }
            putchar(c); //print the next char and repeat        
        }
    
    
        return 0;
    }
    

    I removed the break part as it was not introduced yet in the book,hope this help new comers like me :)

    0 讨论(0)
  • 2021-01-31 20:06
    #include <stdio.h>
    int main()
    {
        int c;
        while( (c = getchar( )) != EOF )
        {           
            if (c == ' ') 
            {
                while ((c = getchar()) == ' ');
                putchar(' ');
                putchar(c);
            }
            else 
                putchar(c);
        }
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-31 20:07

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

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

    }

    0 讨论(0)
  • 2021-01-31 20:08

    Considering what's asked in the question, I have also made sure that the program runs smooth in case of various tabs, spaces as well as when they're clubbed together to form various combinations! Here's my code,

    int c, flag = 1;
        printf("Enter the character!\n");
        while ((c = getchar()) != EOF) {
        if (c == ' '||c == '\t') {
            c=getchar();
            while(c == ' '|| c == '\t')
                   {
                       c = getchar();
                   }
            putchar(' ');
            if (c == EOF) break;
        }
        putchar(c);
    }
    

    Feel free to run all test cases using various combinations of spaces and tabs.

    0 讨论(0)
  • 2021-01-31 20:09

    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;
    }
    
    0 讨论(0)
提交回复
热议问题