Thread: I didn't understand how each word newline program

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    25

    I didn't understand how each word newline program

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        int c;
        int inspace;
    	
        inspace = 1;
        while ((c = getchar()) != EOF)
        {
            if (c == ' ' || c == '\t' || c == '\n')
            {
                if (inspace == 0)
                {
                    inspace = 1;
                    putchar('\n');
                }
    /* else, don't print anything */
            }
            else
            {
                inspace = 0;
                putchar(c);
            }
        }
    	
        return 0;
    }
    I tried this code w/o no "inspace" in it and code still worked but if I made more than one blanks between words it happened vertically double newlines etc.. so I just wanna know how this "inspace" makes newline w/o blank line for each word. Is that a flag ? if it is how compiler handle this please let me know I couldn't understand

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Yes, it's a flag. If it's the first sequential space, tab or newline -> print a newline. If not, do nothing.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    25
    Quote Originally Posted by GReaper View Post
    Yes, it's a flag. If it's the first sequential space, tab or newline -> print a newline. If not, do nothing.
    flag's initial value 1
    then lets say result of "c == ' ' || c == '\t' || c == '\n'"
    equals to 1
    then dont we check the value of inspace if it is 0 and started with inspace was 1 thats where I got confused

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    But if the input is anything else, "inspace" is reset to zero. Practically, by initializing it to one, you ignore every space, tab and newline before the first character.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    25
    Quote Originally Posted by GReaper View Post
    But if the input is anything else, "inspace" is reset to zero. Practically, by initializing it to one, you ignore every space, tab and newline before the first character.
    for every other character other than blank, newline and tab flag turns zero and if it gets blank, newline and tab flag is still zero but inspace==0 becomes TRUE then
    inspace=1 and compiler makes newline is my logic correct ? sorry asking more n more just wanna know how exactly this executed ?

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Exactly.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    25
    Thank you very much!

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This could be called a very simple state machine. It is a important topic in programming or at least it is for embedded programming.

    Finite-state machine - Wikipedia, the free encyclopedia

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For some reason, my program keeps a newline in an integer
    By Shlomi Ardan in forum C Programming
    Replies: 1
    Last Post: 11-03-2012, 04:31 PM
  2. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  3. Program crashing when replacing newline character
    By mdekom12 in forum C Programming
    Replies: 2
    Last Post: 05-01-2010, 08:49 PM
  4. Replies: 4
    Last Post: 08-09-2006, 01:06 PM
  5. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM