removing whitespaces from middle

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mishink7
    New Member
    • Apr 2007
    • 9

    removing whitespaces from middle

    i was wondering how would u remove multiple whitespaces from the middle of a string to make it only one white space...for example:

    Code:
    string hi= "hi     bye";
    then remove the multiple whitespaces in middle to make it "hi bye"

    any help would be appreciated...t hanks.
    Last edited by Ganon11; Apr 6 '07, 12:56 PM. Reason: code tags added to clarify OP's point
  • jj555s
    New Member
    • Mar 2007
    • 36

    #2
    I'm not sure if your example shows multiple whitespace. But, you can try detecting for sequential whitespaces, if true, I forgot which function would work best, strcpy, substr?

    for x = 0; x < strlen (of string); x++)
    if gkfghjk[x] == ' ';
    if fgjfjfghjfj[x+1] == ' ';
    here you do something

    Comment

    • mishink7
      New Member
      • Apr 2007
      • 9

      #3
      yeah my example didnt show it for some reason...i tried substr, but i couldnt get it to work for some reason.

      Comment

      • mishink7
        New Member
        • Apr 2007
        • 9

        #4
        i tried something like this, but its not working

        string trim3(string s)
        {
        int i=0;
        while(i < s.size())
        {
        if (isspace(s[i]))
        {
        int p=0;
        while (isspace(s[i]))
        {
        p++;
        i++;
        }
        s.erase(i, p);
        }
        ++i;
        }
        return s;
        }

        Comment

        • mishink7
          New Member
          • Apr 2007
          • 9

          #5
          ok im trying it the way you told me now...i almost got it i think

          Comment

          • mishink7
            New Member
            • Apr 2007
            • 9

            #6
            thanks a ton...it worked.

            Comment

            Working...