Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Using the isspace function

  1. May 19, 2010 #1
    I am trying to remove excess white space using the isspace function. For example, I am reading text from a txt file that says:

    I am super sleepy.

    ------------------------------
    but I want it to say:

    I am super sleepy.

    ------------------------------

    I was thinking I could use the isspace(c), where c is each character being read and try to make a statement that somehow erases all but one space. Any suggestions??
     
  2. jcsd
  3. May 19, 2010 #2

    Mark44

    Staff: Mentor

    Added [ code] tags make your spaces show up.
    What I would do is read the original sentence into one character array, and then copy characters to another character array. In the copy operation, when I encountered the first space after a word, I would copy that space, but would ignore any subsequent spaces and not copy them. When I hit the first non-space character I would copy it and the subsequent characters up to the first space after the word, and so on, as before.
     
  4. May 20, 2010 #3
    Hmm.. I tried using this segment of code in my program

    Code (Text):
                while (isspace(c))
                {
                    if(!inf.get(c))
                        return false;
                }
    it does delete extra white space, but in the process it also deletes the first character of every word preceded by a white space...

    For example, if the input text file read:

    Why is this program so difficult?

    The output text file prints:

    Why s his rogram o ifficult?

    I don't know what to do to modify my code to fix this problem.....any help would be appreciated!
     
  5. May 20, 2010 #4

    Mark44

    Staff: Mentor

    Last edited by a moderator: Apr 25, 2017
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook