Say I am taking a string in and want to check whether or not it has a capital letter in the string. I input the string from a file. How would I go about breaking it down to check to see if it has an uppercase value using the ascii values? Thanks!
-
`std::find_if`: http://en.cppreference.com/w/cpp/algorithm/find – us2012 Oct 07 '13 at 00:29
-
Please add what you have tried, and what piece of code is giving you trouble. http://stackoverflow.com/help/how-to-ask – Mike D Oct 07 '13 at 00:29
-
I haven't tried anything yet because I cannot think of anything other than possibly taking it in as an array of characters, but that is a lot of overhead. – user2311799 Oct 07 '13 at 00:32
1 Answers
You can use the C++ algorithm library to find if a string contains an uppercase value in it by using a predicate (using a 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 it = std::find_if(str.begin(), str.end(), IsUpper);
if (it != str.end()) {
return true;
}
else {
return false;
}
}
int main() {
std::string s = "This contains an uppercase character in it...";
if (hasUpperCase(s)) {
std::cout << "String s contains a least one uppercase character." << std::endl;
}
else {
std::cout << "String s does not contain any uppercase character." << std::endl;
}
}
From here a predicate is:
is a C++ function returning boolean or instance of object having bool operator() member. Unary predicate take one agrument, binary - two, etc. Examples of questions predicate can answer for particular algorithm are:
- Is this element is what we are looking for?
- Is the first of two arguments is ordered first in our order?
- Are the two arguments equal?
Almost all STL algorithms take predicate as last argument.
So, in summary, this works by each character in the string against the predicate function: IsUpper
which itself uses std::isupper
to do the checking to see if the character in question is uppercase or not. If it is, it returns true, and that character is stored in the iterator.
REFERENCES:
http://en.cppreference.com/w/cpp/algorithm/find https://stackoverflow.com/a/5921826/866930
-
-
Checking if `std::find_if` succeeded consists of comparing the returned iterator to the end iterator. Blindly dereferencing it leads to undefined behavior when a match is not found. Also, what you have now is even less correct than before. Because in addition to the blind dereference, now you are returning a `char` from a function whose return type is `bool`. – Benjamin Lindley Oct 07 '13 at 00:58
-
@BenjaminLindley: correct on all counts. Re-edited. Thanks for the feedback, though - I appreciate it. – jrd1 Oct 07 '13 at 01:03
-
1Still wrong. Let me repeat. Checking if `std::find_if` succeeded consists of comparing the returned iterator to the end iterator. Blindly dereferencing it leads to undefined behavior when a match is not found. Read [the documentation](http://en.cppreference.com/w/cpp/algorithm/find) you provided a link to. There is an example at the bottom. It uses `std::find` instead of `std::find_if`, but the principle is the same. – Benjamin Lindley Oct 07 '13 at 01:09
-
@BenjaminLindley: Ah! I see your point. Edited (hopefully for the last time). Thank you for your patience. – jrd1 Oct 07 '13 at 01:15