Thread: k&r exercise 1-9

  1. #1
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    k&r exercise 1-9

    Code:
    main()
    {
       int c,d;
    
       while((c = getchar()) != EOF)
       {
           d = getchar();
           if(d == ' ')
                   putchar(c);
           else{
                   putchar(c);
                   putchar(d);
           }
        }
    }
    im trying to write a program that replaces one or more blank spaces with a single blank space. this above code works if u enter 2 blank spaces some where in the string. how do i test for 3 or more blank spaces and then replace that space with a single blank space?

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    What your code does is simply not print every other character if it is a space. In other words:
    I AM HERE prints IAM HERE
    and has nothing to do with consecutive spaces.

    What you want to do is
    Code:
    Get the first character (c)
    Output (c)
    Start your while loop reading d
        if c != d  
            output d
        else
            if c != SPACE
                 output d
        c = d
    end loop
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    thanks walt

    thanks guys for responding.

    walt: the code you gave me works, but im having trouble understanding the logic of the code.

    at the end of the loop there is 'c = d' then at the beginning of the loop there is 'if ( c != d)'. if there is 'c = d' at the end of the loop then wouldnt it skip 'if(c != d)' statement and go to else when it looped through the second time?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read the "start your loop" line.

    They're talking about using something like:
    Code:
    while( (c = getchar()) != EOF )
    This line changes the value of c each time through the loop. Thus, if c == d, then you know you've encountered a duplicate character.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    Originally posted by quzah
    Read the "start your loop" line.

    They're talking about using something like:
    Code:
    while( (c = getchar()) != EOF )
    This line changes the value of c each time through the loop. Thus, if c == d, then you know you've encountered a duplicate character.

    Quzah.
    the code of the program that works is :
    Code:
    main()
    {
    	int c,d;
    
    	c = getchar();
    	putchar(c);
    
    	while((d = getchar()) != EOF)
    	{
    		if (c != d)
    			putchar(d);
    		else {
    			if (c != ' ')
    				putchar(d);
    		}
    
    		c = d;
    	}
    }
    with the help of waltp.

    again i ask.

    at the end of the loop there is 'c = d' then at the beginning of the loop there is 'if ( c != d)'. if there is 'c = d' at the end of the loop then wouldnt it skip 'if(c != d)' statement and go to else when it looped through the second time?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Asked and answered.
    At the bottom of the loop, d is assigned to c.
    At the top of the loop, the value of one of those two variables change, so they are no longer the same.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by xion
    the code of the program that works is :
    Code:
    main()
    {
    	int c,d;
    
    	c = getchar();   // Read the first (prev) character of input
    	putchar(c);
    
    	while((d = getchar()) != EOF)   // Read the next character of the input, stop if done
    	{       // Check the prev and next characters
    		if (c != d)
    			putchar(d);
    		else {
    			if (c != ' ')
    				putchar(d);
    		}
    
    		c = d;     // Done with prev so replace with next
    	}
    }
    At the last line, since we are done with the character in c we replace it with the character in d. Now when we read the new d, the comparison is still with the characters adjacent in the input stream

    String: trial
    first comparison c='t' d='r'
    replace: c='r'
    second comparison c='r' d='i'
    replace: c='i'
    etc.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    102
    I would do it like this.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char input;
    
    	input = getchar();
    	while(input != EOF)
    	{
    		if(input == ' ')
    		{
    			while((input = getchar()) == ' ');
    			putch(input);	
    		}
    		else
    			putch(input);
    		input = getchar();
    	}
    	
    	return(0);
    }
    it may be a bit more complex but I really its better then using 2 different variables.

    Mostly what is being done here is looping though the spaces. There is a bug in this though. Im leaving that to xion to find it

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by squid
    I would do it like this.
    Code:
    	char input;
    
    	input = getchar();
    	while(input != EOF)
    it may be a bit more complex but I really its better then using 2 different variables.

    Mostly what is being done here is looping though the spaces. There is a bug in this though. Im leaving that to xion to find it
    Well it may be complex, but it won't work. Don't write buggy code for people to fix, especially when you're already admitting it's more complex, and the only reason for doing so is because you don't feel like using two variables. Bad form. A massive four bytes isn't going to hurt anything at all in a program as simple as this. It's not wise to overly complicate your program just because you don't feel like using another integer.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Yeah, the big bug is it removes all spaces. Serious bug if you only want to remove extra spaces.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Salem
    In that case, there are two bugs then

    > while((input = getchar()) == ' ');
    If this runs into EOF, you're in an infinite loop.
    And for those that didn't spot the bug, go read the FAQ:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    So there are (so far) 4 bugs:
    1) Removes all spaces
    2) Infinite loop if EOF
    3) EOF is not a char
    4) putch() isn't C, putchar() is (at least not in Borland 5.5)

    Amazing how many misteaks can be crammed into 12 lines of C code.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by WaltP
    So there are (so far) 4 bugs:

    4) putch() isn't C, putchar() is (at least not in Borland 5.5)

    Amazing how many misteaks can be crammed into 12 lines of C code.
    It's not ANSI C. It is C. And since they didn't actually add any #include lines, it's possible they are in fact including the correct header for it on their compiler.

    That being said, they really should just use putchar. But assuming their compiler actually has it, and they're including the right header, it's not really an error. It's just not portable.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Mar 2003
    Posts
    102
    Ok, I guess I really need to start practicing again.

  15. #15
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    thanks for all your replies. very much appreciated =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with K&R Book Exercise
    By Alejandrito in forum C Programming
    Replies: 5
    Last Post: 03-11-2008, 01:24 PM
  2. Exercise 2-6 K&R help
    By allix in forum C Programming
    Replies: 19
    Last Post: 08-18-2006, 09:25 AM
  3. K&R Exercise 1-14
    By Lee134 in forum C Programming
    Replies: 3
    Last Post: 02-16-2006, 11:20 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Help with K&R Exercise 1.13
    By Yannis in forum C Programming
    Replies: 2
    Last Post: 09-21-2003, 02:51 PM