Correct use of locales?

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
 
M

Morten Hanssen

Hello, I need to write a small program that can determine if a string is
all
alphabetical characters or not
[...]

void check_string(const std::string& s)
{
size_t digit_count = std::count_if(s.begin(), s.end(), isalpha);

Just writing "isalpha" here will probably use the "C version" of
isalpha(), which only cares about the current globally set locale.

However, there is a std::isalpha() that you could use. It takes the
character as it's first argument, and the locale you want to use as the
second argument. I'm not sure whether you can pass this one to
std::count_if() though, unless you use a functor object, or whatever it is
called. You could just as easily iterate through the string yourself
though, and just call std::isalpha() for each character.

Morten.
 
J

Jeff Schwab

However, there is a std::isalpha() that you could use. It takes the
character as it's first argument, and the locale you want to use as the
second argument. I'm not sure whether you can pass this one to
std::count_if() though, unless you use a functor object, or whatever it
is called.

You can stick the locale onto each call of std::isalpha with
std::bind2nd, like this:

std::count_if( s.begin( ),
s.end( ),
std::bind2nd( std::isalpha, locale ) );

-Jeff

Morten said:
Hello, I need to write a small program that can determine if a string
is all
alphabetical characters or not

[...]

void check_string(const std::string& s)
{
size_t digit_count = std::count_if(s.begin(), s.end(), isalpha);


Just writing "isalpha" here will probably use the "C version" of
isalpha(), which only cares about the current globally set locale.

However, there is a std::isalpha() that you could use. It takes the
character as it's first argument, and the locale you want to use as the
second argument. I'm not sure whether you can pass this one to
std::count_if() though, unless you use a functor object, or whatever it
is called. You could just as easily iterate through the string yourself
though, and just call std::isalpha() for each character.

Morten.
 
M

Morten Hanssen

You can stick the locale onto each call of std::isalpha with
std::bind2nd, like this:

std::count_if( s.begin( ),
s.end( ),
std::bind2nd( std::isalpha, locale ) );

Ah. Neat. :)

Morten.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top