Like Tree2Likes
  • 1 Post By GReaper
  • 1 Post By stahta01

I didn't understand how each word newline program

This is a discussion on I didn't understand how each word newline program within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main( void ) { int c; int inspace; inspace = 1 ; while ((c = getchar ...

  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
    1,616
    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
    1,616
    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.
    amadeus111 likes this.
    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
    1,616
    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
    2,481
    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.
    amadeus111 likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

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

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