Thread: Basic C input output program help

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    15

    Basic C input output program help

    Hey there guys,

    I am new to C programming and even newer to this forum. I am trying to teach my self C programming basics in preparation for when I take the unit at uni for my second semester.
    I am currently working my way through " "The C Programming Language", 2nd edition, Kernighan and Ritchie" and am getting a little stuck on some of the exercises, namely Ex 9 "Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank."
    My first issue was actually understanding what the question was asking of me What I thought the question was asking is that if you input a string of characters and there happened to be more than one space together then it would remove the extra spaces and just have one space. so the input "My____name____is____David" would be output as "My_name_is_David"
    After going to the website http://users.powernet.co.uk/eton/kandr2/krx1.html and looking at the exercise answers I copied and compiled the code below, however the output was not what i expected. The program takes the input and then doubles any spaces in that input. So "My__name__is__David" ends up being output as "My____name____is____David"
    I was just wanting to know anyones opinion on this, have I misunderstood the question ?
    The other point that I would like to get clarified is that I cant understand how the integer "inspace" interacts with the rest of the program. All I see is that "inspace" is either equal to 0 or 1. I do not understand how this integer manages to double the number of spaces in the output. If someone could just break it down for me and explain it step by step I may be able to understand it a little better.
    Any help would be greatly appreciated !!

    Thanks

    Trev

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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This code works as designed: it reduces multiple blanks to one blank.

    The inspace variable is a flag: if you are "in space" (that is, you have been reading spaces) the flag is set, which means further spaces are not output (notice the lack of the putchar statement). If you are reading "normal" characters, the flag is cleared (set to 0).

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    15
    Thanks for the help, after re-typing the code the program worked as it was designed to, and after reading your msg and looking at the program it all fell into place, Cheers !!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  3. Basic Program but I need Help
    By michigan353 in forum C++ Programming
    Replies: 10
    Last Post: 10-25-2005, 07:03 AM
  4. Basic Program but I need HELP
    By michigan353 in forum C Programming
    Replies: 2
    Last Post: 10-23-2005, 03:41 PM