Hello, I have looked around, but I can't seem to find any examples of this. I am relatively new to C++, and was wondering how I could convert a string to upper case. The examples I have found from googling only have to deal with char's, and not with strings.
try the toupper()
function (#include <ctype.h>
). it accepts characters as arguments, strings are made up of characters, so you'll have to iterate over each individual character that when put together comprise the string
#include <algorithm>
#include <string>
std::string str = "Hello World";
std::transform(str.begin(), str.end(),str.begin(), ::toupper);
struct convert {
void operator()(char& c) { c = toupper((unsigned char)c); }
};
// ...
string uc_str;
for_each(uc_str.begin(), uc_str.end(), convert());
Note: A couple of problems with the top solution:
21.5 Null-terminated sequence utilities
The contents of these headers shall be the same as the Standard C Library headers , , , , and [...]
Which means that the
cctype
members may well be macros not suitable for direct consumption in standard algorithms.Another problem with the same example is that it does not cast the argument or verify that this is non-negative; this is especially dangerous for systems where plain
char
is signed. (The reason being: if this is implemented as a macro it will probably use a lookup table and your argument indexes into that table. A negative index will give you UB.)
not sure there is a built in function. Try this:
Include either the ctype.h OR cctype libraries, as well as the stdlib.h as part of the preprocessor directives.
string StringToUpper(string strToConvert)
{//change each element of the string to upper case
for(unsigned int i=0;i<strToConvert.length();i++)
{
strToConvert[i] = toupper(strToConvert[i]);
}
return strToConvert;//return the converted string
}
string StringToLower(string strToConvert)
{//change each element of the string to lower case
for(unsigned int i=0;i<strToConvert.length();i++)
{
strToConvert[i] = tolower(strToConvert[i]);
}
return strToConvert;//return the converted string
}
Boost string algorithms:
#include <boost/algorithm/string.hpp>
#include <string>
std::string str = "Hello World";
boost::to_upper(str);
std::string newstr = boost::to_upper_copy("Hello World");
typedef std::string::value_type char_t;
char_t up_char( char_t ch )
{
return std::use_facet< std::ctype< char_t > >( std::locale() ).toupper( ch );
}
std::string toupper( const std::string &src )
{
std::string result;
std::transform( src.begin(), src.end(), std::back_inserter( result ), up_char );
return result;
}
const std::string src = "test test TEST";
std::cout << toupper( src );
Do you have ASCII or International characters in strings?
If it's the latter case, "uppercasing" is not that simple, and it depends on the used alphabet. There are bicameral and unicameral alphabets. Only bicameral alphabets have different characters for upper and lower case. Also, there are composite characters, like Latin capital letter 'DZ' (\u01F1 'DZ') which use the so called title case. This means that only the first character (D) gets changed.
I suggest you look into ICU, and difference between Simple and Full Case Mappings. This might help:
In all the machines I tested, it was faster. Perhaps because he is not concerned with a very wide range of characters. Or because using switch() it makes a jump table, do not know how it works in the assembly ... just know that is faster :P
string Utils::String::UpperCase(string CaseString) {
for (unsigned short i = 0, tamanho = CaseString.length(); i < tamanho; i++) {
switch (CaseString[i]) {
case 'a':
CaseString[i] = 'A';
break;
case 'b':
CaseString[i] = 'B';
break;
case 'c':
CaseString[i] = 'C';
break;
case 'd':
CaseString[i] = 'D';
break;
case 'e':
CaseString[i] = 'E';
break;
case 'f':
CaseString[i] = 'F';
break;
case 'g':
CaseString[i] = 'G';
break;
case 'h':
CaseString[i] = 'H';
break;
case 'i':
CaseString[i] = 'I';
break;
case 'j':
CaseString[i] = 'J';
break;
case 'k':
CaseString[i] = 'K';
break;
case 'l':
CaseString[i] = 'L';
break;
case 'm':
CaseString[i] = 'M';
break;
case 'n':
CaseString[i] = 'N';
break;
case 'o':
CaseString[i] = 'O';
break;
case 'p':
CaseString[i] = 'P';
break;
case 'q':
CaseString[i] = 'Q';
break;
case 'r':
CaseString[i] = 'R';
break;
case 's':
CaseString[i] = 'S';
break;
case 't':
CaseString[i] = 'T';
break;
case 'u':
CaseString[i] = 'U';
break;
case 'v':
CaseString[i] = 'V';
break;
case 'w':
CaseString[i] = 'W';
break;
case 'x':
CaseString[i] = 'X';
break;
case 'y':
CaseString[i] = 'Y';
break;
case 'z':
CaseString[i] = 'Z';
break;
}
}
return CaseString;
}
//works for ASCII -- no clear advantage over what is already posted...
std::string toupper(const std::string & s)
{
std::string ret(s.size(), char());
for(unsigned int i = 0; i < s.size(); ++i)
ret[i] = (s[i] <= 'z' && s[i] >= 'a') ? s[i]-('a'-'A') : s[i];
return ret;
}