Check if a string contains only alphanumeric characters in C++
This post will discuss how to check if a string contains only alphanumeric characters in C++.
1. Using std::find_if
We can use the std::find_if standard algorithm from the <algorithm> header, which can accept a lambda to find the element in the specified range. It can be used as follows to check if a string contains only alphanumeric characters.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <string> #include <algorithm> bool isAlphaNumeric(const std::string &str) { auto it = std::find_if(str.begin(), str.end(), [](char const &c) { return !isalnum(c); }); return it == str.end(); } int main() { std::string str = "HelloWorld123"; std::cout << std::boolalpha << isAlphaNumeric(str) << std::endl; // true return 0; } |
Since C++11, a better option is to use the std::find_if_not function. Its usage is demonstaed below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <string> #include <algorithm> bool isAlphaNumeric(const std::string &str) { auto it = std::find_if_not(str.begin(), str.end(), [](char const &c) { return isalnum(c); }); return it == str.end(); } int main() { std::string str = "HelloWorld123"; std::cout << std::boolalpha << isAlphaNumeric(str) << std::endl; // true return 0; } |
We can further shorten the above code with isalnum from the global namespace which can be directly referenced with a name:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> #include <algorithm> bool isAlphaNumeric(const std::string &str) { auto it = std::find_if_not(str.begin(), str.end(), ::isalnum); return it == str.end(); } int main() { std::string str = "HelloWorld123"; std::cout << std::boolalpha << isAlphaNumeric(str) << std::endl; // true return 0; } |
2. Using std::all_of
Since C++11, we can use the std::all_of function that returns true if the specified predicate holds for all the elements in the specified range. It can be used as follows to determine if the string contains only alphanumeric characters.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> #include <algorithm> bool isAlphaNumeric(const std::string &str) { return std::all_of(str.begin(), str.end(), [](char const &c) { return std::isalnum(c); }); } int main() { std::string str = "HelloWorld123"; std::cout << std::boolalpha << isAlphaNumeric(str) << std::endl; // true return 0; } |
We can further shorten the above code with isalnum from the global namespace:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <algorithm> bool isAlphaNumeric(const std::string &str) { return std::all_of(str.begin(), str.end(), ::isalnum); } int main() { std::string str = "HelloWorld123"; std::cout << std::boolalpha << isAlphaNumeric(str) << std::endl; // true return 0; } |
3. Using std::count_if
Finally, we can use the std::count_if algorithm to get the count of non-alphanumeric characters in the string. This approach would translate to the following code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <string> #include <algorithm> bool isAlphaNumeric(const std::string &str) { int count = std::count_if(str.begin(), str.end(), [](char const &c) { return !std::isalnum(c); }); return count == 0; } int main() { std::string str = "HelloWorld123"; std::cout << std::boolalpha << isAlphaNumeric(str) << std::endl; // true return 0; } |
This is equivalent to:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> #include <algorithm> bool isAlphaNumeric(const std::string &str) { return std::count_if(str.begin(), str.end(), ::isalnum) == str.length(); } int main() { std::string str = "HelloWorld123"; std::cout << std::boolalpha << isAlphaNumeric(str) << std::endl; // true return 0; } |
That’s all about checking if a string contains only alphanumeric characters in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)