Please explain this to me

This is a discussion on Please explain this to me within the C Programming forums, part of the General Programming Boards category; I was working on a white space compacting program, and I came up with this code which seems to work ...

  1. #1
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528

    Please explain this to me

    I was working on a white space compacting program, and I came up with this code which seems to work nicely:
    Code:
    #include <stdio.h>
    
    /* replace_blanks.c - September 29, 2003
     * by Rob Somers
     * An answer to exercise 1-9 in K&R II
     * If the character is a space, print it, then
     * check the next character, and if it is a space,
     * delete the previous space, and print the new one
     */
    
    int main(void)
    {
             int c;
        
             while( ( c = getchar() ) != EOF ) {
    	        if( ( c == ' ' ) ){
    		         putchar( c );
    	                 while( ( c = getchar() ) == ' ' ){
    			      printf( "\b " );
    	  }		   
          
          }
          putchar( c );
    	 
        }
        return 0;
    }
    However, I was flipping through K&R2 and I saw another way which I tried and it works:
    Code:
    #include <stdio.h>
    
    /* replace_blanks_2.c - September 29, 2003
     * by Rob Somers
     * An answer to exercise 1-9 in K&R II
     */
    
    int main(void)
    {
             int c;
        
             while( ( c = getchar() ) != EOF ) {
    	        if( ( c == ' ' ) ){
    		         putchar( c );
    	                 while( ( c = getchar() ) == ' ' ){
    			      ;
    	  }		   
          
          }
          putchar( c );
    	 
        }
        return 0;
    }
    Now why does the semi-colon work? (The semi-colon replaces the line in red on the first version) Is the while loop saying in effect, "If this condition, do nothing" ? So that way it prints no space after the initial space? The second version looks to be a better solution, not?

    ~/kermit

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >Now why does the semi-colon work?

    It is just a null statement.

    >Is the while loop saying in effect, "If this condition, do nothing" ?

    Yup.
    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.*

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    An answer to exercise 1-9 in K&R II
    If the character is a space, print it, then check the next character, and if it is a space, delete the previous space, and print the new one
    Ahh....What is the purpose of deleting the previous space and then printing a new one?

    The code given in K&R2 is, after detecting a space, print it, and then ignore any following spaces (which is a whole lot more sensical than deleting and then reprinting...)

    Oh...Be aware also that there are white space characters other than ' '. isspace() will identify them.

    Cheers,
    Neil.
    Beware the fury of a patient man.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Actually, there was no code given (all the code given here is mine), just an exercise:
    Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.
    In light of this instruction, I think my original program makes enough sense, even if it is perhaps a little crude. Keep in mind that I did also come up with the second solution which is probably a better one. The first solution was the first one I could get to work.

    There is much learning value in an exercise such as this one, as far as I am concerned. I learned a couple of things the hard way, and such knowledge so hard won will not be easily forgotten.

    I will keep my eyes peeled for the isspace() function...

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    There is much learning value in an exercise such as this one, as far as I am concerned. I learned a couple of things the hard way, and such knowledge so hard won will not be easily forgotten.
    Absolutely. Sorry if I appeared to be bagging you out, it certainly wasn't my intention.

    As for isspace, it is in <ctype.h> and will return a non-zero value for any non-space character (0x09 - 0x0D or 0x20)

    Cheers,
    Neil.
    Beware the fury of a patient man.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Originally posted by Zainny
    Absolutely. Sorry if I appeared to be bagging you out, it certainly wasn't my intention.

    As for isspace, it is in <ctype.h> and will return a non-zero value for any non-space character (0x09 - 0x0D or 0x20)

    Cheers,
    Neil.
    Hey, no worries. Thanks for the info on isspace. I have heard that writing your own functions to replace standard library functions is not exactly time well spent. The exercises in K&R get more advanced as they go on, but I think the beginning ones are more for learning the rudiments of the language, even if they replace standard library functions. Its helpful for guys like me who need things to be rudimentary.

    ~/kermit

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you explain these bitwise operations?
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 10-29-2008, 01:19 PM
  2. Please explain?
    By neo_phyte in forum C Programming
    Replies: 3
    Last Post: 08-25-2006, 05:23 AM
  3. Can someone explain to me what this code means
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2002, 11:36 AM
  4. Replies: 4
    Last Post: 11-19-2002, 08:18 PM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM

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