Thread: Clarification of K&R Exercises

  1. #1
    Registered User edw211's Avatar
    Join Date
    Jun 2011
    Location
    Wilkes-Barre, PA
    Posts
    22

    Clarification of K&R Exercises

    Hello all,

    I'm a college guy trying to bone up on my C skills by progressing through the K&R exercises. Here are the two I am concerned with:

    1-8: Write a program to count blanks, tabs, and newlines.

    My solution...

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int c = 0, blanks = 0, tabs = 0, newlines = 0;
    	
    	while((c = getchar()) != EOF)
    	{
    		switch(c)
    		{
    			case ' ':
    				++blanks;
    				break;
    			case '\t':
    				++tabs;
    				break;
    			case '\n':
    				++newlines;
    				break;
    			default:
    				break;
    		}
    	}
    
    	printf("\nBlanks: %d\nTabs: %d\nNewlines: %d\n\n", blanks, tabs, newlines);
    
    	return 0;
    }
    1-9: Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.

    A solution I found online...

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int c;
      int inspace;
    
      inspace = 0;
      while((c = getchar()) != EOF)
      {
        if(c == ' ')
        {
          if(inspace == 0)
          {
            inspace = 1;
            putchar(c);
          }
        }
    
        /* We haven't met 'else' yet, so we have to be a little clumsy */
        if(c != ' ')
        {
          inspace = 0;
          putchar(c);
        }
      }
    
      return 0;
    }
    My confusion stems from the fact that I can type multiple lines of input before getting an analysis in the first program. However, when I press ENTER for a newline in the second program, the program immediately condenses the line I just typed.

    My understanding of the first exercise, or rather my solution to it, is that an initial call to getchar() is made in the while loop, which waits until I hit ENTER, at which point it processes all the stuff I've entered on that particular line with more calls to getchar() because there is more stuff waiting in the text stream. But, nothing is printed because I don't print anything until I break out of the while loop with the EOF signal. Then, it calls getchar() again to wait for more characters and another press of ENTER because I've yet to enter the EOF signal (Ctrl-Z on my setup using Pelles C). First of all, is that a correct assessment of what's going on?

    I'm more confused by the solution I encountered to the second exercise, but please tell me if this is correct: Again, I have a while loop that is waiting until I enter the EOF signal, but getchar() is also initially waiting until I hit the ENTER key the first time it is called. When I finally do hit ENTER, the processing of the line I've entered begins. More or less, if I've encountered a space and am already "in space" I simply don't display that extra space, and use the inspace variable to keep track of that condition. Is that all correct?

    Mainly, I'm concerned with why I can enter multiple lines before anything happens in the first program and why things immediately happen when I ask for a newline in the second program. I just want to know if the answer I came up with for that is right based on my readings.

    Thanks,
    -Evan Williams
    Last edited by edw211; 06-08-2011 at 03:14 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Your first while loop isn't printing anything at all, until you signal EOF.

    Your second loop is printing as it goes. Since input is normally line buffered, you type in a line of text, press enter, and then your program loop runs for that line of input (and produces some output), before waiting for the next complete line of input.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User edw211's Avatar
    Join Date
    Jun 2011
    Location
    Wilkes-Barre, PA
    Posts
    22
    Okay cool; I think that's pretty much what I was trying to say, but in less words. Thanks for the confirmation, Salem. Glad I finally understand that.

    Cheers =)
    -Evan Williams

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Exercises
    By GReaper in forum C++ Programming
    Replies: 11
    Last Post: 07-17-2010, 01:22 PM
  2. C# Exercises
    By Rainbowinblack in forum C# Programming
    Replies: 0
    Last Post: 01-17-2008, 01:47 PM
  3. C++ Exercises
    By lasher in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2006, 09:48 PM
  4. Exercises
    By blankstare77 in forum C++ Programming
    Replies: 2
    Last Post: 08-31-2005, 05:46 PM
  5. Exercises
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2001, 11:29 AM