Thread: Easy Exercise 1

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    12

    Easy Exercise 1

    Hello guys, I'm a beginner in programming. I've just come across the problem in K&R2: Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.I also found a solution, but i have some questions about it. First: what does it mean "inspace=0 ?" what is done by this? and what does " inspace=1" mean? How does the program affect blanks?
    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;
    }
    i have questions about the second solution. what does it mean at the end "pc=c"? for what reason is it used? and could you explain why we take "pc=EOF"? what happens with this assignment? Sorry for my easy questions, but i really could not find answers to these.
    Code:
    #include <stdio.h>
    
    /* count lines in input */
    int
    main()
    {
            int c, pc; /* c = character, pc = previous character */
    
            /* set pc to a value that wouldn't match any character, in case
            this program is ever modified to get rid of multiples of other
            characters */
    
            pc = EOF;
    
            while ((c = getchar()) != EOF) {
                    if (c == ' ')
                            if (pc != ' ')   /* or if (pc != c) */ 
                                    putchar(c);
    
                    /* We haven't met 'else' yet, so we have to be a little clumsy */
                    if (c != ' ')
                            putchar(c);
                    pc = c;
            }
    
            return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2015
    Posts
    12
    please help me

  3. #3
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    There is a big difference between writing a solution and finding a solution on the web.

    The first one forces you to work out the problem. I suggest you try that; who knows, it might even be fun.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. REALLY NEED HELP with this exercise.
    By madzior771 in forum C Programming
    Replies: 1
    Last Post: 12-26-2012, 05:41 PM
  2. An exercise from K&R
    By GL.Sam in forum C Programming
    Replies: 4
    Last Post: 05-07-2010, 09:31 AM
  3. K&R exercise
    By Tool in forum C Programming
    Replies: 1
    Last Post: 12-05-2009, 09:39 AM
  4. Seg fault in easy, easy code
    By lisa1901 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2007, 05:28 AM
  5. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM