K&R Exercise 1-9

This is a discussion on K&R Exercise 1-9 within the C Programming forums, part of the General Programming Boards category; Exercise 1-9. Write a program to copy its input to its output, replacing each string of one or more blanks ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    4

    K&R Exercise 1-9

    Exercise 1-9. Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.

    I guess it very simple but I tried many combinations ... I managed to do this program last year it was by chance I guess because I cant do it now , but I failed on the 1-10 when I checked the answers from internet I saw that commands were used which wasnt mentioned at all in the program till 22nd page ... that ........ed me off and then I left the book now I started again because I am thinking there shall be many ways to do a program and surely there would be solutions with the mentioned commands till 22nd , right now I sucked at this , gonna read the last paragraphs again and continue trying it in this time I would appreciate any suggestions from you and here is my idea to the program :

    If there is more than one space convert all to a single space and continue ...

    here is the basic i/o code from K&R :

    Code:
    #include <stdio.h>
    /* copy input to output; 1st version */
    main()
      {
      int c;
      c = getchar();
      
      while (c != EOF) {
      putchar(c);
      c = getchar();
      }
    }
    P.S.: I am a complete beginner.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is how I might do it, on a string literal. If it was a char array, I'd probably do the same, but use an index perhaps, instead of a pointer.

    I don't like the mess of getc, getchar, fgetc, fgetchar, etc.. They're too close to being the same, but not. Sort of not hot, not cold, just lukewarm blah and you want to spit them out.

    IMO, pointers and indeces are natural iterators for char strings and char arrays.

    Code:
    #include <stdio.h>
    /* copy input to output; 1st version */
    
    
    int main()
      {
      int skip;
      char *c;
      char *string = { "\nWater, water,   everywhere,\nand all the boards did shrink\n    water, water,  everywhere   and nary a drop to   drink"};
    
      printf("\n\n %s \n", string);
    
      c = string;
      skip = 0;
      while (*c != '\0') {
        if(*c == ' ' && skip == 1)
          ;                                        //we be skipping! :)
        else {
          putchar(*c);
          if(*c != ' ')
            skip = 0;
        }
        ++c;
        
        if(*c == ' ' && skip == 0) {
          skip = 1;
          putchar(*c);
          ++c;
        }
      }
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    4
    thanks a lot for your time but && operator and * isnt yet mentioned in the book

    P.S. :I am not able to give any input in your program

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I have to confess, the exercises in K&R, seemed straight forward, or uninteresting, so I never did them.

    OK, you haven't had pointers yet. How about indexes (indeces), for working in arrays?

    Do you want char's from a file, or do you want them from the keyboard?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Adak View Post
    Do you want char's from a file, or do you want them from the keyboard?
    I believe a number of the K&R exercises like this expect a file to be piped to the stdin.

    @Anarchy: spoiler alert, but there's not a whole lotta code there.
    Last edited by Dave_Sinkula; 01-10-2010 at 10:16 AM. Reason: Added link to online solution.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        /*Write a program to copy its input to its output, replacing each string of
          one or more blanks by a single blank. */
          
          int c;
          
          enum { in_space, out_space };
          int state = in_space;
          
          while((c=getchar())!=EOF)
          {
             if(c==' ' || c=='\t')
             {
                 if(state == out_space)
                 putchar(' ');
                 
                 state = in_space;
             }
             else if(c=='\n') 
             {
                  state = in_space;
                  putchar(c);
             }
             else
             {
                 state = out_space;
                 putchar(c);
             }
          }    
         
      
        
      system("PAUSE");	
      return 0;
    }
    Exercises in K&R boring :O Ofcourse if you've readen about 5+ books.

    Its my first book and i find it awsome.

    Altho i must admit, my code looks terrible now when i look at it . It's been a while since i wrote this.
    Last edited by Tool; 01-10-2010 at 12:36 PM.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    4
    Hey guys thanks a lot but I read the answer again which was on internet and I finally understood it :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. programming exercise
    By mashour06 in forum C Programming
    Replies: 1
    Last Post: 06-01-2009, 07:22 AM
  2. Line of data input method
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 12:34 AM
  3. Creating a Sorted Linked List, Help Please!
    By larry_2k4 in forum C Programming
    Replies: 4
    Last Post: 04-28-2009, 02:12 AM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21