5

I have a std::string and wish for the first letter to be capitalized and the rest lower case.

One way I could do this is:

const std::string example("eXamPLe");
std::string capitalized = boost::to_lower_copy(example);

capitalized[0] = toupper(capitalized[0]);

Which would yield capitalized as:

"Example"

But perhaps there is a more straight forward way to do this?

WilliamKF
  • 41,123
  • 68
  • 193
  • 295

3 Answers3

6

If the string is indeed just a single word, std::string capitalized = boost::locale::to_title (example) should do it. Otherwise, what you've got is pretty compact.

Edit: just noticed that the boost::python namespace has a str class with a capitalize() method which sounds like it would work for multi word strings (assuming you want what you described and not title case). Using a python string just to gain that functionality is probably a bad idea, however.

jerry
  • 2,581
  • 1
  • 21
  • 32
  • 1
    The header file for `boost::locale` is `#include ` – WilliamKF Mar 12 '13 at 19:38
  • @WilliamKF yes, sorry for not pointing that out. Also, I know you're not concerned with internationalization, but for anyone who is, be warned that this can give different results in different locales (and the same caveat applies to the original code snippet) – jerry Mar 12 '13 at 19:42
  • I'm not finding `boost/locale.hpp` in my build where other headers like `boost/lambda/lambda.hpp` and `boost/algorithm/string.hpp` are present. Does this mean I have an old version of boost or just one without all parts installed? – WilliamKF Mar 12 '13 at 19:46
  • What version of boost do you have? According to the history, the locale library was introduced in 1.48.0 (released on November 15th, 2011). There have been updates since as recently as last month, so it's a good idea to update. – jerry Mar 13 '13 at 00:53
0

A boost-less solution is:

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

int main()
{
    const std::string example("eXamPLe");

    std::string s = example;    
    s[0] = toupper(s[0]);
    std::transform(s.begin()+1, s.end(), s.begin()+1, tolower);
    
    std::cout << s << "\n";
}
JBen
  • 108
  • 4
-1

I think the string variable name is example and the string stored in it is "example". So try this:

example[0] = toupper(example[0]);
for(int i=1 ; example[i] != '\0' ; ++i){
        example[i] = tolower(example[i]);
        }

cout << example << endl;

This might give you the first character CAPITALIZED and the rest of the string becomes lowercase. It's not quite different from the original solution but just a different approach.

  • 2
    This would indeed capitalize the first letter, but leave the rest unchanged. If you read the question, you'll see that they want the first letter in upper-case and the rest in lower-case. Since you can't assume the other letters are lower-case, the answer is incorrect. – Josef Ginerman Jul 13 '20 at 20:16
  • I guess you are incorrect about your logic. Try compiling the below program and u can see the results for yourself. #include //#include using namespace std; #define ll long long #define mod 10000007 const int mx=100; int main(){ ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0); #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif string str; cin >> str; str[0] = toupper(str[0]); cout << str << endl; return 0; } – Siddharth S Jul 13 '20 at 20:18
  • Your example works because you are using all-lowercase words to test it. Try it with the example in the question ("eXamPLe") and you'll see what I mean :-) – Josef Ginerman Jul 13 '20 at 20:21
  • It still works! Did you compile in your computer. Please do try! I just did, and it worked for the given word: "eXamPLe". – Siddharth S Jul 13 '20 at 20:28
  • asking for "the rest lower case", not "the rest of the string remains the same"... – B. Go Jul 13 '20 at 20:33
  • In you own answer, you wrote: "This might give you the first character CAPITALIZED and the rest of the string remains the same." Remains the same. Code does what you tell it to do, nothing more, and in the snippet you sent there is no place where you fix the rest of the letters... Anyway, just wanted to help out. Good day! – Josef Ginerman Jul 13 '20 at 20:34
  • A little advice, the loop condition `i <= example.length() - 1` isn't very idiomatic and includes a potential bug. You most commonly see the condition as `i < example.length()`, note the `<` instead of `<=`. Both versions are logically the same but using less than means you don't do `example.length() - 1`. That member function returns an **unsigned** type and, if the string is empty, subtracting 1 would wrap around to a very large value. That's a terrible bug and is easily avoided buy using `<` instead of `<=` combined with that subtraction. – Blastfurnace Jul 13 '20 at 21:04