473,386 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Correct use of locales?

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
3 1560
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Adrian Parker | last post by:
I have a server app which is sent a date in the local format of the client machine. I've no control over the client app at all, so have to code at the server end to cope with any problems. The...
0
by: Christian Marko | last post by:
Hi to all! As I read in the bug report on the Sun website, the bug, that java and javaw uses different default locales should be fixed with 1.4.2, but I use 1.4.2_04 and this bug still occurs. ...
1
by: Moof | last post by:
Hi there, I'm trying to use some country-specific services provided by python under Windows, and I'm coming up with a couple of problems, specifically that floats are not being printed correctly...
6
by: David Opstad | last post by:
I have a question about text rendering I'm hoping someone here can answer. Is there a way of doing linguistically correct rendering of Unicode strings in Python? In simple cases like Latin or...
2
by: garykpdx | last post by:
Is there some sort of tutorial on locales or the locale module? I can't seem to find a list showing all possible locales. I made 'en' work alright, but when I tried 'de' or 'de_DE' or 'es_ES',...
3
by: J Trauntvein | last post by:
I was working with a co-worker the other day to work through the process of formatting numeric values by imbueing C++ iostreams with locales. His program's initialisation code had a call to...
3
by: Joshua Stewart | last post by:
Hi, I am trying to characterise a problem I am seeing on our C#/C++ xml driven application. We have recently added some basic Spanish language support to our application, but it seems that there...
8
by: Rik | last post by:
Hi all, is there a in PHP to get the available locales without shellacces (i.e. locale -a)? Grtz, -- Rik Wasmus
4
by: Gilles Ganault | last post by:
Hello What does it take to support locales? The following test code found on the PHP site doesn't work as planned: ======= setlocale(LC_TIME, "C"); echo strftime("%A"); setlocale(LC_TIME,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.