27

I have a string: "apple". How can I convert only the first character to uppercase and get a new string in the form of "Apple"?

I can also have a string with multibyte characters.

What if the first character of the string is a multibyte character ?

Ouroborus
  • 16,237
  • 4
  • 39
  • 62
user1065276
  • 287
  • 1
  • 3
  • 6

5 Answers5

55
string str = "something";
str[0] = toupper(str[0]);

That's all you need to do. It also works for C strings.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • Do you mean `char* str = "something"; str[0] = toupper(str[0]);` would work? (as this is c-string). – Nawaz Dec 16 '11 at 06:33
  • 2
    @Nawaz not that because that would be modifying a const char[9] (in memory you can't modify). But if you had a C string in some memory you own then yes it will work. – Seth Carnegie Dec 16 '11 at 06:37
11

Like what Carneigie said,

string str = "something";
str[0] = toupper(str[0]);

but also remember to:

#include <string>
#include <cctype>

all the way up

HoKy22
  • 4,057
  • 8
  • 33
  • 54
  • I don't understand. Why do we need to include `cctype`? This is a C header, and it's best practise to stick with C++ headers only with C++. Also, the code above doesn't require `cctype`. ??? – ThatPixelCherry Apr 07 '16 at 11:13
  • 1
    @ThatPixelCherry It's for C++ as well. http://www.cplusplus.com/reference/cctype/ toupper requires cctype – HoKy22 Apr 07 '16 at 13:33
8

I cannot use str[0] because, I can have string which has multibyte characters

I don't know of any CRT implementation that supports non-ASCII character classification and conversion. If you want to support Unicode then everything is much more complicated since "converting the first character to uppercase" may be meaningless in other languages. You have to use a Unicode library written by experts for this.

To illustrate how complicated it is, consider the following case in English. Converting the three code-point sequence 'file' (with f-i ligature) shall break the first codepoint into two separate letters resulting in 'File'. Please note that the standard C/C++ interfaces for doing case classification and conversion don't take such cases into account, so it's even impossible to implement them to support Unicode correctly.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
4
#include <iostream>
using namespace std;

void capitalize (string &s)
{
    bool cap = true;

    for(unsigned int i = 0; i <= s.length(); i++)
    {
        if (isalpha(s[i]) && cap == true)
        {
            s[i] = toupper(s[i]);
            cap = false;
        }
        else if (isspace(s[i]))
        {  
            cap = true;
        }
    }
}
Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
Rye Bryant
  • 41
  • 1
3

(Only works with 'ASCII' characters.)

std::wstring s = L"apple";

if(islower(s.at(0) <= 'z' ? s.at(0) : 'A'))
    s[0] += 'A' - 'a';

Or if you are feeling fancy and feel like torturing any future readers of your code:

std::wstringstream wss;
wss << std::uppercase   << s[0]
    << std::nouppercase << s.substr(1);
wss >> s;
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
  • I cannot use str[0] because, I can have string which has multibyte characters – user1065276 Dec 16 '11 at 06:32
  • 3
    The fancy form is rather ugly and slow. Better warn him NOT to do this way. – Nawaz Dec 16 '11 at 06:34
  • I don't think this will work with multibyte characters because adding 'A' - 'a' won't necessarily convert some multibyte character to uppercase. It will only work for ASCII, no? – Seth Carnegie Dec 16 '11 at 06:47
  • @SethCarnegie Indeed. To be specific, Latin-1 only. What about the second piece of code, though? – Mateen Ulhaq Dec 16 '11 at 06:49
  • No idea, never dealt with multibyte strings before. – Seth Carnegie Dec 16 '11 at 06:52
  • `std::uppercase` does not what you think it does. According to http://en.cppreference.com/w/cpp/io/manip/uppercase it **only works in floating-point and hexadecimal integer output** – Timo Mar 29 '17 at 15:52