Thread: how to skip white space?

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    14

    Lightbulb how to skip white space?

    hello,

    In a string, the is white spaces.
    In print output, I want to print without whitespaces(print only characters).
    how can i do it.

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    There are a few ways. One would be to print the data one character at a time, checking to see whether it is a space using the isspace() function. You may have to include the ctype.h header file to use that function.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    14

    Thumbs up again print to avoid whitespace!

    Sir,Thanks for your reply.
    I am already know about isspace() fn.
    But, after check condition if(isspace(char str)),
    how can I print the characters with out space.ie., how to avoid the white space.

  4. #4
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    That's easy, just use the not operator.

    Code:
    if( !isspace( str ) )
    {
        printf("%c", str );
    }
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You know you can reply rather than just start a new thread every time you mean to reply...


    Quzah.
    Last edited by quzah; 06-14-2006 at 04:21 AM.
    Hope is the first step on the road to disappointment.

  6. #6
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Wow, that's some good caps aversion while still getting across the same feeling of caps. :P
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  7. #7
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    U can even use something like :



    Code:
     i=0;
     while(string[i]!='\0')
         {
              if(string[i] == ' ')
                  i++;
    
            
              printf("%c",string[i]);
              i++;
    
    
        }

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That will only skip white space character at a time. Also, if the last character in the string is a space, you'll skip to the null, then print the null, then advance past the end of the string, and then ... keep on chugging through your memory, in spaces you shouldn't be playing in.


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

  9. #9
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    yeah, U were right about the last character being null.It did not strike me. He wants to skip all the white characters right? so, other than the case where the last char is a space ,this shud work..or will it?

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, your code won't work. What if there are two spaces in a row?

    The issapce() should be in a while loop.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. remove white space
    By pokemon in forum C Programming
    Replies: 12
    Last Post: 03-10-2009, 03:12 PM
  2. compare function ignoring leading white space
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-23-2008, 01:33 PM
  3. read characters till white space
    By ashok449 in forum C Programming
    Replies: 2
    Last Post: 03-08-2008, 03:56 AM
  4. Remove white space problem
    By jeffdavis_99 in forum C++ Programming
    Replies: 8
    Last Post: 03-30-2005, 09:29 PM