Thread: Blank Character Constant Problem

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    36

    Blank Character Constant Problem

    I need help interpreting this problem:

    Exercise 1-9. Write a program to copy its input to its output, replacing each string of one or
    more blanks by a single blank.
    I'm interpreting this to mean that I will input a character string with at least one instance of multiple spaces such as "abc---defg----hijkl-----mnop" (I had to use dashes instead of spaces for this post) and the job of my program will be to consolidate all those instances to a single space.

    The program I have so far fails to do this:

    Code:
    /* consildate multiple blanks in input to one blank */
    
    #include <stdio.h>
    
    main()
    {
    	int c, b;
    
    	b = 0;
    
    	while ((c = getchar()) != EOF)
    	{
    		if (c != ' ')
    		{
    			putchar(c);
    			b = 0;
    		}
    		if (c == ' ')
    		{
    			++b;
    			if (b = 1)
    			{
    				putchar(c);
    			}
    			if (b > 1)
    			{
    				;
    			}
    		}
    	}
    }
    Do I not understand the problem or do I need to rework my code?

    Thank you.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    you mention that the code fails to do what you want but forgot to say what it does. it's easier to figure out why a program does something wrong when you know what that something is. "if (b = 1)" is a red flag though. i think you meant == instead of =.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    			if (b = 1)
    is TRUE always, since you are SETTING b to 1 every time you hit this point in the code. Use two equal signs to compare.

    To avoid this mistake, you may want to use the "equals backwards" method:
    Code:
    			if (1 == b)
    That way, if you happen to write only one = sign, the compiler will cry out loud and clear that you got it wrong.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I read it the same way you do.
    But you have a bug in the code, in one of the ifs.
    And main returns int. Always. Not nothing. Int.
    Finally, b & c are not very descriptive variable names.

    Some compilers will warn about assignments in if statements, if you only set the warnings it emits to max.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    Thank you both.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    Thank you all.

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You understand it well enough (the problem that is) so it is all in the implementation now. Basically you want to define two states (a state machine), 'in' a word, and 'outside' of a word. I think that is maybe the approach your were attempting. Start by assuming that you are not in a word, and get a character. Test it. If it is a ' ' or a '\t', ignore it, and move on to the next character. Repeat. When you get a non-whitespace character, now you are 'in' a word (or 'out' of a space, if you like). Change the state to reflect that. Print that character. Get the next one. Test it. If it is not whitespace, print it. Do this until you get to a whitespace. When you get to a whitespace, print a ' '. Then change the state to 'out', and discard all the following whitespace (if any) characters. Continue on like this until you reach EOF.

    Exercise 1-12 is somewhat similar, except that it writes one word per line. Basically it inserts '\n' instead of ' '. Have a look at this for an example of what I described in the paragraph above. Just keep in mind that it is not the same program, but does has some pertinent ideas. If you want some good solutions to the exercises (sometimes it doesn't hurt to have a look after you have tried what you can to solve the problem), Richard Heathfield keeps a site with some here.
    Last edited by kermit; 02-10-2009 at 03:09 PM.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by matsp View Post
    Code:
    			if (b = 1)
    is TRUE always, since you are SETTING b to 1 every time you hit this point in the code. Use two equal signs to compare.

    To avoid this mistake, you may want to use the "equals backwards" method:
    Code:
    			if (1 == b)
    That way, if you happen to write only one = sign, the compiler will cry out loud and clear that you got it wrong.


    --
    Mats
    if you can remember to put the constant first, you can remember to use the right operator. the final nail for me is that the trick only works if the left hand side of the expression isn't an lvalue. no amount of uglifying your code will fix "int a, b; ... if (a = b)", so the whole putting the constant on the left side is just a false sense of security that encourages stupid mistakes like "int a, b; ... if (b = a)".

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Meldreth View Post
    if you can remember to put the constant first, you can remember to use the right operator. the final nail for me is that the trick only works if the left hand side of the expression isn't an lvalue. no amount of uglifying your code will fix "int a, b; ... if (a = b)", so the whole putting the constant on the left side is just a false sense of security that encourages stupid mistakes like "int a, b; ... if (b = a)".
    And of course, you've never had to try to find where the code goes wrong because:
    Code:
    err = somefunction();
    if (err = NO_ERROR) ...
    where the function DOES return an error, but you never discover (and things are going horribly wrong later, because something that somefunction should have done is not being done in full).

    It is not about "remember to use ==", it is about "spotting when you got it wrong". And yes, it's not a surefire way for everything, as you say, if (a = b) will still assign one variable with the other if you reverse it.

    It still helps. [Although I must admit I only use it when writing driver code - not user-mode code].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    Quote Originally Posted by kermit View Post
    You understand it well enough (the problem that is) so it is all in the implementation now. Basically you want to define two states (a state machine), 'in' a word, and 'outside' of a word. I think that is maybe the approach your were attempting. Start by assuming that you are not in a word, and get a character. Test it. If it is a ' ' or a '\t', ignore it, and move on to the next character. Repeat. When you get a non-whitespace character, now you are 'in' a word (or 'out' of a space, if you like). Change the state to reflect that. Print that character. Get the next one. Test it. If it is not whitespace, print it. Do this until you get to a whitespace. When you get to a whitespace, print a ' '. Then change the state to 'out', and discard all the following whitespace (if any) characters. Continue on like this until you reach EOF.

    Exercise 1-12 is somewhat similar, except that it writes one word per line. Basically it inserts '\n' instead of ' '. Have a look at this for an example of what I described in the paragraph above. Just keep in mind that it is not the same program, but does has some pertinent ideas. If you want some good solutions to the exercises (sometimes it doesn't hurt to have a look after you have tried what you can to solve the problem), Richard Heathfield keeps a site with some here.
    Thanks for those links.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem about Character Constant of \t
    By userpingz in forum C++ Programming
    Replies: 3
    Last Post: 06-24-2009, 03:30 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Problem accepting a character
    By babu in forum C Programming
    Replies: 7
    Last Post: 08-27-2007, 12:26 AM
  4. Comparing Character Arrays problem...
    By newy100 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2003, 07:54 PM
  5. c++ problem with character manipulation
    By scrapper777 in forum C++ Programming
    Replies: 3
    Last Post: 11-03-2003, 11:48 PM