Removing Puctuation from Strings

This is a discussion on Removing Puctuation from Strings within the C++ Programming forums, part of the General Programming Boards category; Hello, I am having som problems with a program I am writing. I am to remove punctuation from words read ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    85

    Removing Puctuation from Strings

    Hello, I am having som problems with a program I am writing.

    I am to remove punctuation from words read in from a file.

    "-" are ok

    EX.
    Code:
    infile >> temp_word;
    
    for(i=0; i<strlen(temp_word.c_str());i++)
    {
          if(isalpha(temp_word[i])
          {
                  temp_word[i] = temp_word[i];
          }
    }
    The strings print out with
    Code:
     cout << temp_word;
    but they have punctuation in them still


    PLEASE HELP


    Also I wouls like to use STL's like
    Code:
     isalpha()
    if poss.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Look up the remove_if STL function (in the <algorithm> header) and the ispunct function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    I tried using an
    Code:
    (!ispucnct)
    but it didn't do anything

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    First, the string class has a length() method, there is no need to use strlen, just call temp_word.length().

    Second, your problem is that you are assigning the character to itself in the same string. You need two variables, the source and the target. If the character is not punctuation (!ispunct(temp_word[i])), then add it to the target string (using += would be easiest). Otherwise don't add it. The resulting string will have the punctuation removed.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    I found the answer on google:

    Code:
    size_t pos; // Position holder
    
    while( (pos = s.find_first_of("`~!@#$%^&*()-_=+{}|[]\\:;\"\'<>,.?/\t\r\n")) != string::npos)
    s.erase(pos, 1);
    where s = name of the string

  6. #6
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    looks very inefficient to me, since it keeps looping through the whole string until all punctuation is removed. I'd say do it all in a single pass with a for loop and use a stringstream for output or something.. something like this....

    Code:
    std::stringstream buff;
    
    for (int i=0; i<s.size(); i++)
    {
        if (isalpha(s[i]))
            buff << s[i];
    }
    
    return buff.str();

  7. #7
    Registered User byte's Avatar
    Join Date
    Aug 2005
    Posts
    8
    Have you tried using ascii's integer equivalent? Using if statement, test whether a particular element belongs to a certain range of punctuation symbols (ascii).

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    I think the solution that hk_mp5kpdw had in mind would be to define:
    Code:
    bool isPunctChar(char ch) {
    	return std::ispunct(ch);
    }
    Then for a std::string str use:
    Code:
    str.erase(std::remove_if(str.begin(), str.end(), isPunctChar), str.end());
    After including the <algorithm>, <cctype> and <string> standard headers.

    std::ispunct() takes an int, so we define isPunctChar() to take a char.

    std::remove_if() operates on a range and removes the characters in str for which isPunctChar() returns true. Okay, it doesnt quite remove them, but now you have a substring of characters that dont contain the unwanted characters, and a substring of characters that may be discarded. std::remove_if() returns an iterator to this range of unwanted characters, so str.erase() is used to, er, erase them.

    Of course, your (original) requirements dont exactly match std::ispunct() since you allow dashes, but then isPunctChar() can be modified to allow them.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Removing Specific Characters from Strings
    By gfmartin05 in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2009, 08:53 AM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  3. removing characters from strings
    By DeepBlackMagic in forum C++ Programming
    Replies: 1
    Last Post: 04-27-2003, 09:33 PM
  4. Removing spaces from strings
    By PunkyBunny300 in forum C Programming
    Replies: 6
    Last Post: 02-21-2003, 01:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21