Thread: K&R Exercise 1.9: Removing spaces at beginning of string

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    K&R Exercise 1.9: Removing spaces at beginning of string

    Hello all,

    I'm teaching myself C and have begun working through K&R for most of the obvious reasons out there.

    On exercise 1.9, about replacing multiple blanks with single blanks, I get my code to work. Continuing in the spirit of the exercise, I tried to remove all spaces at the beginning of the input. So far I've been successful in replacing multiple beginning spaces with a single space, but I can't make that one space go away. If I type a single space at the beginning, it stays there as well.

    I checked for answers and found the following two, but neither addresses spaces at the beginning.

    http://cboard.cprogramming.com/c-programming/41766-k-r-exercise-1-9-a.html


    The C Programming Language Exercise 1-9

    Usual beginner restriction applies: can only use what I've learned so far (except for the int main()/return 0 deal, which seems to be a big deal).

    Here's my code. What is it doing wrong?

    Code:
    /* Program to copy its input to its output, replacing each string of one or
       more blanks by a single blank. */
    
    #include <stdio.h>
    
    int main()
    {
      int c, b; /* c = current character, b = previous character */
      
      b == ' '; /* control for space-as-first-character at the begining of input */
    
      while((c = getchar()) != EOF) /* loop sequentially through input until EOF */
        {
    
          if (c == ' ') /* space case: check if character is space */
    	{if (b == ' ') /* b is space: do nothing, step forward the loop */
    	  {
    	    ; /* do nothing */
    	  }
    	}
    
          if (c == ' ') /* space case: check if b is space */
            {if (b != ' ') /* b is not space: display character */
    	  putchar(c);
    	}
    
          if (c != ' ') /* normal case: display the character */
    	{
    	  putchar(c);
    	}
          b = c; /* Store value of character before steping forward */
        }
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In your loop, you have logic to keep one space inside the string of char's. You don't have the logic in there to handle any leading spaces, in any different way.

    Work it through by hand and see what you come up with.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I think your problem is here:

    Code:
    b == ' '; /* control for space-as-first-character at the begining of input */
    You are not assigning space to b you are comparing it. Should be a single = only.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    2
    Quote Originally Posted by Subsonics View Post
    I think your problem is here:

    Code:
    b == ' '; /* control for space-as-first-character at the begining of input */
    You are not assigning space to b you are comparing it. Should be a single = only.
    That did the trick! Thanks! Now to make some sense of this, a single equal sign (=) means that I make the left-hand side take the value of the right hand side (or assign the right-hand side value to the left-hand-side variable), while a double-equal sign (==) asks if the left hand side is equivalent to the right-hand side.

    Quote Originally Posted by Adak View Post
    In your loop, you have logic to keep one space inside the string of char's. You don't have the logic in there to handle any leading spaces, in any different way.

    Work it through by hand and see what you come up with.
    Thanks to you as well! After I fixed the initial condition (b = 0) instead of (b == 0), I noticed that I only had logic to handle the leading spaces for the first string after the program executed. I fixed it by resetting b to 'space' after each newline character. It works now.

    Here's the updated code.

    Code:
    /* Program to copy its input to its output, replacing each string of one or
       more blanks by a single blank. */
    
    #include <stdio.h>
    
    int main()
    {
      int c, b; /* c = current character, b = previous character */
      
      b = ' '; /* control for space-as-first-character at the beginning of input */
    
      while((c = getchar()) != EOF) /* loop sequentially through input until EOF */
        {
    
          if (c == ' ') /* space case: check if character is space */
    	{if (b == ' ') /* b is space: do nothing, step forward the loop */
    	  {
    	    ; /* do nothing */
    	  }
    	}
    
          if (c == ' ') /* space case: check if b is space */
            {if (b != ' ') /* b is not space: display character */
    	  putchar(c);
    	}
    
          if (c != ' ') /* normal case: display the character */
    	{
    	  putchar(c);
    	}
    
          b = c; /* Store value of character before stepping forward */
         
          if (c == '\n') /* end-of-line check: reset to b = ' ' to handle leading spaces*/
    	{
    	  b = ' ';
    	}
        }
      return 0;
    }
    Also, I find the code hard to read (as in ugly). Not sure if it's the braces or the misaligned comments. Do you have any suggestions I should pick up before I develop really bad habits?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM