W
William Payne
Hello, I need to write a small program that can determine if a string is all
alphabetical characters or not (not by using a loop, I am using count_if(),
other suggestions welcome). However, I can't make it work for the Swedish
alphabet (which contains three characters not found in the English
alphabet). Here's the code:
#include <algorithm>
#include <iostream>
#include <locale>
#include <string>
void check_string(const std::string& s);
int main(int /*argc*/, char** /*argv*/)
{
std::locale loc("se");
check_string("a1c");
check_string("åäö");
check_string("abc");
return 0;
}
void check_string(const std::string& s)
{
size_t digit_count = std::count_if(s.begin(), s.end(), isalpha);
if(digit_count == s.length())
{
std::cout << s << " all alphabetical characters." << std::endl;
}
else
{
std::cout << s << " not all alphabetical characters." << std::endl;
}
}
When I run this program, the output is:
$ ./check_string.exe
a1c not all alphabetical characters.
åäö not all alphabetical characters.
abc all alphabetical characters.
the "åäö" string should be recognised as all alphabetical characters. How do
I correct this error?
/ William Payne
alphabetical characters or not (not by using a loop, I am using count_if(),
other suggestions welcome). However, I can't make it work for the Swedish
alphabet (which contains three characters not found in the English
alphabet). Here's the code:
#include <algorithm>
#include <iostream>
#include <locale>
#include <string>
void check_string(const std::string& s);
int main(int /*argc*/, char** /*argv*/)
{
std::locale loc("se");
check_string("a1c");
check_string("åäö");
check_string("abc");
return 0;
}
void check_string(const std::string& s)
{
size_t digit_count = std::count_if(s.begin(), s.end(), isalpha);
if(digit_count == s.length())
{
std::cout << s << " all alphabetical characters." << std::endl;
}
else
{
std::cout << s << " not all alphabetical characters." << std::endl;
}
}
When I run this program, the output is:
$ ./check_string.exe
a1c not all alphabetical characters.
åäö not all alphabetical characters.
abc all alphabetical characters.
the "åäö" string should be recognised as all alphabetical characters. How do
I correct this error?
/ William Payne