string

This is a discussion on string within the C++ Programming forums, part of the General Programming Boards category; if i have a string that contains a puncuation marks. How do i discard that puncuation mark ? for example: ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    55

    string

    if i have a string that contains a puncuation marks. How do i discard that puncuation mark ?

    for example:

    programming.

    how do i make it into:

    programming << exclude the fullstop

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Create a loop, and systematically go through each character in the string, looking for a punctuation mark. Then, skip outputting that character, and continue. Like this:
    Code:
    const char * String = "pro.gramming.is.great...";
    for (int X=0; String[X] != '\0'; X++)
    {
        if (String[X] != '.')
        {
            cout << String[X];
        }
    }

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    ^ that's a good idea, but I would do it like this:
    Code:
    ...
    const char*puctuation=".,></?\\\"\':;|[]{}()!@#$%^&*~`-_=+";
    
    int length=strlen(phrase);
    int punclength=strlen(punctuation);
    
    for(int x=0;x<length;x++)  //walk through phrase taken in
    {
       for(int y=0;y<punclength;y++)  //walk through puncuation defs
       {
          if(phrase[x]==puctuation[y])  //if current char is punctuation
              removechar(phrase,x);  //call function to remove it
       }
    }
    ...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Use the remove_if function along with the ispunct function.
    Code:
    #include <string>
    #include <algorithm>
    #include <iostream>
    #include <ctype.h>
    using namespace std;
    
    int main()
    {
        string str("H.!ell,o' ;Wor?ld:");
        cout << str << endl;
        str.erase(remove_if(str.begin(),str.end(),ispunct),str.end());
        cout << str << endl;
        return 0;
    }
    Output:
    H.!ell,o' ;Wor?ld:
    Hello World
    "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

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    oh yeah... forgot about the ispucnt function... I used my way because when I wrote it I needed to know what punctuation I was taking out (I needed to keep some of it in)
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Better yet, a localized ispunct function.

    Code:
    #include <string>
    #include <locale>
    #include <algorithm>
    #include <functional>
    
    template <typename C>
    void remove_punct(std::string &s, const std::locale &loc = std::locale())
    {
      s.erase(std::remove_if(str.begin(), str.end(), std::bind2nd(std::ptr_fun(std::ispunct<C>), loc), s.end());
    }
    I'm not entirely sure if this will compile as it is.
    Last edited by CornedBee; 01-21-2004 at 02:16 PM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Long live C!!!

    Code:
     char * purge(char str[], const char * disc)
    {
     char *ret, *p;
         for(p=ret=str;*str!=0; ++str)  {
          bool copy=true;
             for(const char * d =disc; *d != 0; ++d)  {
                 if(*d == *str) copy=false;
           }
            if(copy)  {
           *p++=*str;
          }
       }
     *p=0;
     return ret;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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