checking for printable characters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • qazmlp

    checking for printable characters

    How do you check whether an std::string object contains only printable characters ?
  • Russell Hanneken

    #2
    Re: checking for printable characters

    "qazmlp" <qazmlp1209@red iffmail.com> wrote in message
    news:db9bbf31.0 307222033.b283b 7d@posting.goog le.com...[color=blue]
    > How do you check whether an std::string object contains only printable
    > characters ?[/color]

    I think this would work:

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

    namespace util
    {
    bool isAllPrintable (std::string const &s)
    {
    typedef std::unary_nega te<
    std::pointer_to _unary_function <int, int> > NotFunc;
    NotFunc isNotPrintable( std::ptr_fun(st d::isprint));
    return std::find_if(s. begin(), s.end(), isNotPrintable) ==
    s.end();
    }
    }

    --
    Russell Hanneken
    rhanneken@pobox .com


    Comment

    • John Harrison

      #3
      Re: checking for printable characters


      "qazmlp" <qazmlp1209@red iffmail.com> wrote in message
      news:db9bbf31.0 307222033.b283b 7d@posting.goog le.com...[color=blue]
      > How do you check whether an std::string object contains only printable[/color]
      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


      Comment

      Working...