Like Tree1Likes
  • 1 Post By laserlight

program reducing numerous spaces to one

This is a discussion on program reducing numerous spaces to one within the C Programming forums, part of the General Programming Boards category; Here is the code Code: #include <stdio.h> main () { int c, lastc; while ((c = getchar()) != EOF) { ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    39

    Question program reducing numerous spaces to one

    Here is the code
    Code:
    #include <stdio.h>
    
    main ()
    {
        int c, lastc;
        
        while ((c = getchar()) != EOF) {
            if (c != ' ')
                putchar(c);
            if (c == ' ')
                    if (lastc != ' ')
                        putchar(c);
            lastc = c;
        }            
    }
    That's an example from K&R, but I can't understand how it can work.
    The line with lastc is incomprehensible to me.
    How can it check if lastc (which is actually nothing) is different from space?
    Thanks in advance !

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    19,386
    You are right: lastc should have been initialised to say, a null character. It is not that lastc is nothing at that point in the first iteration, but that it was not initialised.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    39
    Quote Originally Posted by laserlight View Post
    You are right: lastc should have been initialised to say, a null character. It is not that lastc is nothing at that point in the first iteration, but that it was not initialised.
    Thanks, but when I'd introduce eg. lastc = 0;
    how can it be compared with blank space? 0 is always different than ' '
    What's the point of that if then?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    19,386
    Quote Originally Posted by Pole
    how can it be compared with blank space? 0 is always different than ' '
    What's the point of that if then?
    Refer to this line:
    Code:
    lastc = c;
    Pole likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    19,386
    Maybe it would be easier to understand like this:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int lastc = '\0'; /* previous character */
        int c; /* current character */
    
        while ((c = getchar()) != EOF) {
            if (c != ' ' || lastc != ' ') {
                putchar(c);
            }
            lastc = c;
        }
    
        return 0;
    }
    If the current character is not a space, then obviously you should print it because it would not be a consecutive space. Or, if the previous character was not a space, then printing the current character (a space) is fine because it would not be a consecutive space. Hence, consecutive spaces are not printed, and thus it appears that they have been reduced to a single space.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is there any way for a program to find out how many spaces...?
    By Programmer_P in forum C++ Programming
    Replies: 24
    Last Post: 01-17-2012, 11:16 PM
  2. Program does not printf after spaces.
    By team23 in forum C Programming
    Replies: 5
    Last Post: 09-18-2010, 03:46 PM
  3. Replies: 7
    Last Post: 10-03-2009, 10:58 PM
  4. Problem with reducing fraction program
    By Turtal in forum C Programming
    Replies: 12
    Last Post: 12-07-2007, 01:25 AM
  5. Replies: 4
    Last Post: 12-01-2007, 03:10 PM

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