checking for printable characters

R

Russell Hanneken

qazmlp said:
How do you check whether an std::string object contains only printable
characters ?

I think this would work:

#include <algorithm>
#include <cctype>
#include <functional>
#include <string>

namespace util
{
bool isAllPrintable (std::string const &s)
{
typedef std::unary_negate<
std::pointer_to_unary_function<int, int> > NotFunc;
NotFunc isNotPrintable(std::ptr_fun(std::isprint));
return std::find_if(s.begin(), s.end(), isNotPrintable) ==
s.end();
}
}
 
J

John Harrison

qazmlp said:
How do you check whether an std::string object contains only printable
characters ?

bool printable = true;
for (string::const_iterator i = s.begin(); i != s.end(); ++i)
{
if (!isprint(*i))
{
printable = false;
break;
}
}

Why anyone would prefer using something from <algorithm> beats me, but each
to his own.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top