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
Printable View
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
}
}
...
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
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)
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());
}
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;
}