By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

Correct use of locales?

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Sat, 13 Dec 2003 16:20:13 +0100, William Payne
<mi******************@student.liu.se> wrote:
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.
Jul 22 '05 #2

P: n/a
> 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 Hanssen wrote: On Sat, 13 Dec 2003 16:20:13 +0100, William Payne
<mi******************@student.liu.se> wrote:
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.


Jul 22 '05 #3

P: n/a
On Sat, 13 Dec 2003 15:57:06 -0500, Jeff Schwab <je******@comcast.net>
wrote:
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.
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.