C++ Checking String -


say taking string in , want check whether or not has capital letter in string. input string file. how go breaking down check see if has uppercase value using ascii values? thanks!

you can use c++ algorithm library find if string contains uppercase value in using predicate (using wrapper version of std::isupper):

#include <algorithm> #include <iostream> #include <string> #include <cctype>  bool isupper (char c) {     return std::isupper(c); }  bool hasuppercase (std::string str) {     std::string::iterator = std::find_if(str.begin(), str.end(), isupper);     if (it != str.end()) {         return true;     }     else {         return false;     } }  int main() {     std::string s = "this contains uppercase character in it...";     if (hasuppercase(s)) {         std::cout << "string s contains least 1 uppercase character." << std::endl;      }     else {         std::cout << "string s not contain uppercase character." << std::endl;      } } 

from here predicate is:

is c++ function returning boolean or instance of object having bool operator() member. unary predicate take 1 agrument, binary - two, etc. examples of questions predicate can answer particular algorithm are:

  • is element looking for?
  • is first of 2 arguments ordered first in our order?
  • are 2 arguments equal?

almost stl algorithms take predicate last argument.

so, in summary, works each character in string against predicate function: isupper uses std::isupper checking see if character in question uppercase or not. if is, returns true, , character stored in iterator.

references:

http://en.cppreference.com/w/cpp/algorithm/find https://stackoverflow.com/a/5921826/866930


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -