Removing Spaces and Punctuations From a Line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bigbenwallace
    New Member
    • Nov 2006
    • 5

    Removing Spaces and Punctuations From a Line

    I was wondering if someone can show me how to remove spacing and punctuations from a line of input using character functions(such as is punct., is space) I also want to use a for loop to do it. I just don't know where to start on this. I think I should use if statements stating if(i is space) then remove it but im not sure how too do that.

    Thanks,
  • sivadhas2006
    New Member
    • Nov 2006
    • 142

    #2
    Hi,

    Can you post your program whatever you have tried to do that?

    Regards,
    M.Sivadhas.

    Comment

    • bigbenwallace
      New Member
      • Nov 2006
      • 5

      #3
      yeah no problem.
      for(i=0; i<str.size(); ++i)
      {
      if(i is punct.)
      str.erase(i,1)
      if(i is space)
      str.erase(i,1)
      }

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        That is not that bad a start, however you only want to increment i if you haven't erased a character

        Comment

        • bigbenwallace
          New Member
          • Nov 2006
          • 5

          #5
          I think I am going to create a temporary string:

          string CleanString (string old, int& numspaces, int& numpunct)
          {
          string newold;
          for(int i=0; i<old.size(); ++i)
          {

          if(!isspace(old[i]) || !ispunct(old[i]))
          newold = newold+old[i];
          }

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by bigbenwallace
            I think I am going to create a temporary string:
            Not a bad plan but

            1. initialise newold to ""

            2. newold = newold+old[i]; would be better written

            newold += old[i];

            as it doesn't envolve the creation of a temporary string for the expression.

            Comment

            • bigbenwallace
              New Member
              • Nov 2006
              • 5

              #7
              [QUOTE=Banfa]Not a bad plan but

              1. initialise newold to ""

              2. newold = newold+old[i]; would be better written

              newold += old[i];

              as it doesn't envolve the creation of a temporary string for the expression.[/QUOTE

              Thanks for the help

              Comment

              Working...