Convert first letter in string to uppercase
Asked Answered
E

5

29

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 ?

Evangelista answered 16/12, 2011 at 6:22 Comment(6)
google.com/search?q=Title+case+a+stringHardpan
The language I'm using is C++Evangelista
google.com/search?q=Title+case+string+c%2B%2BHardpan
Amazing this isn't a duplicate.Hardpan
@RobertHarvey I suppose we can't mark 'convert char to uppercase' as a duplicate?Lancelancelet
This is a tricky thing to do in a completely general way. It requires specific knowledge of the encoding, a precise definition of "character", and sometimes even knowing the culture/language of the (part of the) string you're modifying. You'll almost certainly need some amount of library help.Interfuse
O
56
string str = "something";
str[0] = toupper(str[0]);

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

Overturf answered 16/12, 2011 at 6:27 Comment(2)
Do you mean char* str = "something"; str[0] = toupper(str[0]); would work? (as this is c-string).Dunbarton
@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.Overturf
E
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

Em answered 17/2, 2013 at 1:48 Comment(2)
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. ???Strachey
@Strachey It's for C++ as well. cplusplus.com/reference/cctype toupper requires cctypeEm
P
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.

Parliamentarian answered 16/12, 2011 at 6:41 Comment(0)
B
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;
        }
    }
}
Biltong answered 24/1, 2013 at 5:13 Comment(1)
Perfect, just what I was looking for. This also takes multiple words with spaces.Chickadee
L
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;
Lancelancelet answered 16/12, 2011 at 6:25 Comment(6)
I cannot use str[0] because, I can have string which has multibyte charactersEvangelista
The fancy form is rather ugly and slow. Better warn him NOT to do this way.Dunbarton
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?Overturf
@SethCarnegie Indeed. To be specific, Latin-1 only. What about the second piece of code, though?Lancelancelet
No idea, never dealt with multibyte strings before.Overturf
std::uppercase does not what you think it does. According to en.cppreference.com/w/cpp/io/manip/uppercase it only works in floating-point and hexadecimal integer outputRouen

© 2022 - 2024 — McMap. All rights reserved.