Thread: Eliminating extra spaces between words

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    20

    Eliminating extra spaces between words

    Hi all, I'm quite new to C programming, so forgive me if I've made silly mistakes. I'm tryin to write a program to remove the extra spaces between words.

    For example:
    Code:
    Input: C Programming        is    fun. 
    Output: C Programming is fun.
    So far, I'm only able to remove all spaces, and I had a problem with the output of my program. The program would print funny characters at the end of the output. I would greatly appreciate if anyone could point out to me what is wrong, and how I could further implement my code to meet my objectives. (like the above example). Thank you!

    Code:
       char input[50];
       char output[50];
       int i=0, j=0;
    
       printf("Input = ");
       fgets(input, 50, stdin);
      
       while(input[i]!='\0')
       {
          if(isspace(input[i]))
             i++; 
          else if(!isspace(input[i]) && input[i]!='\0')
          {
    	      output[j] = input[i];
    	      i++;
    	      j++;
          }
       }
       
       puts(output);
    When I run my program, I get
    Code:
    Input = there     is   a
    Output = thereisa(funny characters here)

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    You need to check for more than one space in a row and terminate the output string with '\0', that's what's causing the funny characters at the end.

    Code:
       while(input[i])
       {
          if(isspace(input[i]) && isspace(input[i+1]))
             i++; 
          else 
          {
              output[j] = input[i];
              i++;
              j++;
          }
       }
       output[j] = '\0'
    Last edited by Scarlet7; 03-15-2005 at 07:06 AM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    20
    Yeap thx! I know that i need to check for more than 1 space in a row, but how do I implement it? I've been trying, but seem not to be able to code it without getting any errors.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    or like this
    Code:
    do
       {
          if(isspace(input[i]) && isspace(input[i+1]))
             i++; 
          else 
          {
              output[j] = input[i];
              i++;
              j++;
          }
       }while(input[i-1]);

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    20
    Thanks a lot Quantum1024! It works perfectly. Could I know why the alogorithm is done in that manner? I'm a bit confused about having a do while loop there. Thanks a lot cos I wanna learn more. =)

  6. #6
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    or like this, using pointers..

    Code:
       char *pi = input;
       char *po = output;
    
       do
       {
          if(isspace(*pi) && isspace(*(pi+1)))
             pi++; 
          else 
              *(po++) = *(pi++);
       }while(*(pi-1));

  7. #7
    Registered User coolshyam's Avatar
    Join Date
    Mar 2005
    Posts
    26

    Cool my logic

    i have seen the usage of isspace function.thats nice.
    but you can use the ascii value of space instead.the ascii value of space is 10 i suppose.so
    Code:
    while(input[i]!=\0)
    {
               if(input[i]==10)
               {
                      count++;
               }
               if(count!=2)
               {
                         output[i]=input[i];
               }
               else if(count[i]==2)
               {
                          continue;
               }
    }
    well may be there might be some syntax errors.but i believe the logic to be correct
    Last edited by coolshyam; 03-15-2005 at 09:21 AM. Reason: spellin mistake

    any questions any type in programming

    a ready made answer
    -----------------------------------------------------------------------------------
    FORTUNE FAVOURS THE BOLD!
    -----------------------------------------------------------------------------------

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The logic isn't correct if you aren't on an ASCII encoded machine.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Furthermore, we can make it nice and pretty by doing this instead:
    Code:
        while( *in )
        {
            while( !isspace( ( *out++ = *in++ ) ) );
            while( isspace( *in ) ) in++;    
        }
    Well, it's fun anyway.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help, i cant count spaces or words
    By cdosrun in forum C Programming
    Replies: 2
    Last Post: 11-25-2005, 03:13 PM
  2. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 12:03 AM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  4. deleting extra spaces
    By girlzone in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2003, 02:06 PM