Removing punctuation marks

Hello,

Here's just a small snippet of code that correctly removes punctuation marks from "hello." Please explain what line 14 is doing. I have no idea! Also what is ispunct and where is it defined? The reference documentation for string::erase and replace_if are not helping at all.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main (void)
{
	string _word("h!@#$el%^&lo*(/");
	
	cout << "Before removing punctuation marks:" << endl;
	cout << "_word = " << _word << endl;

	_word.erase(remove_if(_word.begin(), _word.end(), ispunct ), _word.end());

	cout << "After removing punctuation marks:" << endl;
	cout << "_word = " << _word << endl;
}


Also, how would I test whether a _word contains ONLY punctuation marks, and whether it contains a space?

Thanks :)
Last edited on
man ispunct wrote:
character classification routines
1
2
#include <ctype.h> //c
#include <cctype> //c++ 

ispunct() checks for any printable character which is not a space or an alphanumeric character.


http://www.cplusplus.com/reference/algorithm/remove_if/
1
2
template < class ForwardIterator, class Predicate >
ForwardIterator remove_if ( ForwardIterator first, ForwardIterator last, Predicate pred );

pred
Unary predicate taking an element in the range as argument, and returning a value indicating the falsehood (with false, or a zero value) or truth (true, or non-zero) of some condition applied to it. This can either be a pointer to a function or an object whose class overloads operator().

Also, how would I test whether a _word contains ONLY punctuation marks
\exists x \in S, \neg Pred(x)
Topic archived. No new replies allowed.