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
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
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]; } }
^ 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
Use the remove_if function along with the ispunct function.
Output: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; }
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
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
Better yet, a localized ispunct function.
I'm not entirely sure if this will compile as it is.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()); }
Last edited by CornedBee; 01-21-2004 at 03: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
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; }
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }