checking for printable characters

Discussion in 'C++' started by qazmlp, Jul 23, 2003.

  1. qazmlp

    qazmlp Guest

    How do you check whether an std::string object contains only printable characters ?
    qazmlp, Jul 23, 2003
    #1
    1. Advertising

  2. "qazmlp" <> wrote in message
    news:...
    > 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();
    }
    }

    --
    Russell Hanneken
    Russell Hanneken, Jul 23, 2003
    #2
    1. Advertising

  3. "qazmlp" <> wrote in message
    news:...
    > 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
    John Harrison, Jul 23, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    0
    Views:
    398
    qazmlp
    Jul 23, 2003
  2. Pascal
    Replies:
    3
    Views:
    673
    Roel Mathys
    Dec 4, 2003
  3. Pascal
    Replies:
    0
    Views:
    283
    Pascal
    Dec 3, 2003
  4. Daniel Alexandre
    Replies:
    2
    Views:
    559
    Sibylle Koczian
    Mar 21, 2005
  5. metaperl
    Replies:
    1
    Views:
    298
    Peter Otten
    Feb 9, 2007
Loading...

Share This Page