Check if a string contains only letters in C++
This post will discuss how to check if a string contains only letters in C++.
1. Using string::find_first_not_of
We can use the string::find_first_not_of function to check the absence of a character in the string. It returns string::npos if the string does not contain any of the specified characters. To check if a string contains only letters, do as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> bool containsOnlyLetters(std::string const &str) { return str.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") == std::string::npos; } int main() { std::string str = "String"; std::cout << std::boolalpha << containsOnlyLetters(str) << std::endl; // true return 0; } |
2. Using string::find_if
The std::find_if algorithm returns an iterator to the first element in the specified range for which the specified predicate returns true. If the predicate returns false for all elements, the function returns an iterator to the end of the specified range. We can use a combination of std::find_if and std::isalpha to match for all alphabetic characters, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> #include <algorithm> #include <cctype> bool containsOnlyLetters(std::string const &str) { auto it = std::find_if(str.begin(), str.end(), [](char const &c) { return !std::isalpha(c); }); return it == str.end(); } int main() { std::string str = "String"; std::cout << std::boolalpha << containsOnlyLetters(str) << std::endl; // true return 0; } |
We can further shorten the code using adaptors from the <functional> header:
|
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> #include <functional> #include <cctype> bool containsOnlyLetters(std::string const &str) { return std::find_if(str.begin(), str.end(), std::not1(std::ptr_fun((int(*)(int))std::isalpha))) == str.end(); } int main() { std::string str = "String"; std::cout << std::boolalpha << containsOnlyLetters(str) << std::endl; // true return 0; } |
3. Using std::all_of
The std::all_of function returns true if all of the elements of the supplied range are true according to a predicate function. It is available since C++11 and can be used as follows to check if a string contains only alphabets.
|
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 containsOnlyLetters(std::string const &str) { return std::all_of(str.begin(), str.end(), [](char const &c) { return std::isalpha(c); }); } int main() { std::string str = "String"; std::cout << std::boolalpha << containsOnlyLetters(str) << std::endl; // true return 0; } |
4. Using std::regex_match
Finally, we can use a regular expression to check whether a string contains only letters. Starting with C++11, we can use std::regex_match to match a sequence against a regular expression.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> #include <regex> bool containsOnlyLetters(std::string const &str) { return std::regex_match(str, std::regex("^[A-Za-z]+$")); } int main() { std::string str = "String"; std::cout << std::boolalpha << containsOnlyLetters(str) << std::endl; // true return 0; } |
That’s all about checking if a string contains only letters 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 :)