[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

C++ - how to convert string to uppercase/lowercase

Michal

12/17/2008 5:30:00 PM


Hallo
I looked through ANSI/ISO C++ standard string, and I did not find any
function from string class that would do so. Did I overlooked
something or it is so?

regards,
Michal
50 Answers

Victor Bazarov

12/17/2008 5:45:00 PM

0

Michal wrote:
> I looked through ANSI/ISO C++ standard string, and I did not find any
> function from string class that would do so. Did I overlooked
> something or it is so?

The conversions are in std::locale, or simply use 'toupper' (and
'tolower') function from the standard C library. Look in the newsgroup
archives for a multitude of proposed solutions for your problem.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

sean_in_raleigh

12/17/2008 8:55:00 PM

0

On Dec 17, 12:29 pm, Michal <rabbi...@tenbit.pl> wrote:
> Hallo
> I looked through ANSI/ISO C++ standard string, and I did not find any
> function from string class that would do so. Did I overlooked
> something or it is so?

Here's the STL-ish way.

//////
#include <cctype>
#include <algorithm>
#include <string>

int my_toupper(int c)
{
return toupper(c);
}

int
main(int argc, char **argv)
{
using namespace std;
string s = "hello world";
transform(s.begin(), s.end(), s.begin(), my_toupper);
}
////

You have to create my_toupper (or use a cast) due to a
C++ wart.

Sean

Kai-Uwe Bux

12/17/2008 9:35:00 PM

0

Michal wrote:

>
> Hallo
> I looked through ANSI/ISO C++ standard string, and I did not find any
> function from string class that would do so. Did I overlooked
> something or it is so?

Maybe something like:

#include <locale>
#include <string>

template < typename Char, typename Traits >
std::basic_string< Char, Traits > &
to_lower ( std::basic_string< Char, Traits > & str,
std::locale loc = std::locale() ) {
typedef std::basic_string< Char, Traits > string;
typedef std::ctype< Char > char_type;
char_type const * the_type_ptr = &std::use_facet< char_type >( loc );
for ( typename string::size_type i = 0; i < str.size(); ++i ) {
str[i] = the_type_ptr->tolower( str[i] );
}
return ( str );
}

#include <iostream>
#include <ostream>

int main ( void ) {
std::string msg ( "Hello World!" );
to_lower( msg );
std::cout << msg << '\n';
}


Best

Kai-Uwe Bux


Noah Roberts

12/17/2008 9:47:00 PM

0

sean_in_raleigh@yahoo.com wrote:

> You have to create my_toupper (or use a cast) due to a
> C++ wart.

What wart?


#include <cctype>
#include <algorithm>
#include <string>
#include <iostream>

int main()
{
std::string x = "hello world";
transform(x.begin(), x.end(), x.begin(), toupper);
std::cout << x << std::endl;
}


Works fine in VS and G++.

Jeff Schwab

12/17/2008 9:55:00 PM

0

Michal wrote:
> I looked through ANSI/ISO C++ standard string, and I did not find any
> function from string class that would do so. Did I overlooked
> something or it is so?

It's a hard thing to do correctly, because case conversion means
different things in different locales. I found this article informative:

http://lafstern.org/matt/co...

If you just want a quick & dirty ASCII conversion, you can always loop
over the input string's characters and call ::toupper on each of them.
Else, if you have access to boost, try its string algorithm library:

http://www.boost.org/doc/libs/1_37_0/doc/html/string_algo/usage.html...
http://www.boost.org/doc/libs/1_37_0/doc/html/string_algo/reference.html#header.boost.algorithm.string.cas...

Btw, we've already had some holywa-- debates in this group re. the
limitations of the standard library's approach to case conversion,
particularly regarding conversions that change the length of the string
(either in characters, or in bytes of a given encoding).

sean_in_raleigh

12/17/2008 10:05:00 PM

0

On Dec 17, 4:46 pm, Noah Roberts <n...@nowhere.com> wrote:
> sean_in_rale...@yahoo.com wrote:
> > You have to create my_toupper (or use a cast) due to a
> > C++ wart.
>
> What wart?
> [...]
> Works fine in VS and G++.

Well, you changed the program I posted, so
presumably you know the one I mean!

That said, you might not consider the
fact that importing std:: into your
main() causes it to break to be a wart,
but I do.

Sean

Sam

12/17/2008 11:25:00 PM

0

sean_in_raleigh@yahoo.com writes:

> On Dec 17, 12:29 pm, Michal <rabbi...@tenbit.pl> wrote:
>> Hallo
>> I looked through ANSI/ISO C++ standard string, and I did not find any
>> function from string class that would do so. Did I overlooked
>> something or it is so?
>
> Here's the STL-ish way.
>
> //////
> #include <cctype>
> #include <algorithm>
> #include <string>
>
> int my_toupper(int c)
> {
> return toupper(c);
> }
>
> int
> main(int argc, char **argv)
> {
> using namespace std;
> string s = "hello world";
> transform(s.begin(), s.end(), s.begin(), my_toupper);
> }
> ////

That's not very STL-ish.

> You have to create my_toupper (or use a cast) due to a
> C++ wart.

No you don't.

std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun(toupper));

works just fine. /That/ is the STL-ish way.


Jeff Schwab

12/17/2008 11:33:00 PM

0

Sam wrote:
> sean_in_raleigh@yahoo.com writes:
>
>> On Dec 17, 12:29 pm, Michal <rabbi...@tenbit.pl> wrote:
>>> Hallo
>>> I looked through ANSI/ISO C++ standard string, and I did not find any
>>> function from string class that would do so. Did I overlooked
>>> something or it is so?
>>
>> Here's the STL-ish way.
>>
>> //////
>> #include <cctype>
>> #include <algorithm>
>> #include <string>
>>
>> int my_toupper(int c)
>> {
>> return toupper(c);
>> }
>>
>> int
>> main(int argc, char **argv)
>> {
>> using namespace std;
>> string s = "hello world";
>> transform(s.begin(), s.end(), s.begin(), my_toupper);
>> }
>> ////
>
> That's not very STL-ish.
>
>> You have to create my_toupper (or use a cast) due to a
>> C++ wart.
>
> No you don't.
>
> std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun(toupper));
>
> works just fine. /That/ is the STL-ish way.

The non-wart in question is also easily avoided by qualifying the name
as ::toupper.

alfps

12/17/2008 11:37:00 PM

0

On 17 Des, 22:34, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> Michal wrote:
>
> > Hallo
> > I looked through ANSI/ISO C++ standard string, and I did not find any
> > function from string class that would do so. Did I overlooked
> > something or it is so?
>
> Maybe something like:
>
> #include <locale>
> #include <string>
>
> template < typename Char, typename Traits >
> std::basic_string< Char, Traits > &
> to_lower ( std::basic_string< Char, Traits > & str,
>            std::locale loc = std::locale() ) {
>   typedef std::basic_string< Char, Traits > string;
>   typedef std::ctype< Char > char_type;
>   char_type const *  the_type_ptr = &std::use_facet< char_type >( loc );
>   for ( typename string::size_type i = 0; i < str.size(); ++i ) {
>     str[i] = the_type_ptr->tolower( str[i] );
>   }
>   return ( str );
>
> }

This is indeed the *intended* way for C++ level conversion.

Unfortunately MinGW g++ 3.4.5 for Windows lacks locale support, hence
the above won't uppercase e.g. Norwegian characters.

The practical solution is to use the C library's toupper function
(MinGW g++ uses Microsoft's runtime library which does this
correctly).

Others have posted such code that won't work in general.

It's important to remember to add a setlocale( "", LC_ALL ) (or
perhaps the arguments are in the opposite order, check).


>
> #include <iostream>
> #include <ostream>
>
> int main ( void )

This void is a C-ism, best a-voided in C++.


> {
>   std::string msg ( "Hello World!" );
>   to_lower( msg );
>   std::cout << msg << '\n';
>
> }


Cheers & hth.,

- Alf

Juha Nieminen

12/18/2008 12:07:00 AM

0

Noah Roberts wrote:
> transform(x.begin(), x.end(), x.begin(), toupper);

Does the standard guarantee that toupper() will always be a function
and never a preprocessor macro?