1

How do convert a string to lowercase except for the first characters in C++? Can this be completed with STL?

Thanks

std::transform(words.begin(), words.end(), words.begin(), ::tolower);   
Justin
  • 177
  • 8
  • 1
    I'd just iterate through the string and apply [tolower()](https://www.cplusplus.com/reference/cctype/tolower/) on each character I want to change. – paulsm4 Jan 07 '22 at 06:51
  • 1
    Can you convert an entire string to lowercase? What's your method? What is the obstacle to skipping the first character of the string? – JaMiT Jan 07 '22 at 06:51
  • 1
    https://en.cppreference.com/w/cpp/algorithm/transform. Well you may use `std::transform`, but I think you can simply make a loop to do so. – Yves Jan 07 '22 at 06:53
  • Yes, I have succeeded to convert everything to lowercase, however, I don't want the first char in the string to be converted into lowercase. – Justin Jan 07 '22 at 06:53
  • 1
    "Yes, I have succeeded to convert everything to lowercase", good. So that "0" which you use in that code, what happens if you replace it by a "1"? – Yunnosch Jan 07 '22 at 06:55
  • It works now, Thanks to everyone for helping out! – Justin Jan 07 '22 at 06:57
  • 1
    @Justin *"Yes, I have succeeded to convert everything to lowercase"* -- that answers my first question. My second question was a request for you to edit that code into your question. And that still leaves my third question... – JaMiT Jan 07 '22 at 07:01
  • 1
    Here is the same question, answered : https://stackoverflow.com/questions/20018961/capitalize-the-first-letter-lower-case-the-rest – FreudianSlip Jan 07 '22 at 07:03
  • Sure! I will write my code in the descriptioin! (= Thanks for the feedback – Justin Jan 07 '22 at 07:03
  • Thank you very much for sharing it! I appreciate your help! – Justin Jan 07 '22 at 07:05
  • Yes, it certainly helps me, I love to read more. Thank yu so much for sharing! – Justin Jan 07 '22 at 07:07
  • 1
    `std::tolower` should *not* be passed to `std::transform` directly, see an explanation [here](https://en.cppreference.com/w/cpp/string/byte/tolower#Notes). – Evg Jan 07 '22 at 07:43
  • Thanks for your respond. I will check it out! (= – Justin Jan 07 '22 at 18:40

1 Answers1

1

You can just transform [1:n] character, and do not change the first character. Code like this:

#include <iostream>
#include <string>
#include <algorithm>    // transform
using namespace std;

int main()
{
   string str = "FbcdADcdeFDde!@234";
   transform(str.begin()+1, str.end(), str.begin()+1, ::tolower);
   cout << str << endl;
   return 0;
}

  • 2
    You must be careful if you use `std::transform` because if `str` is empty, it will throw an exception. That's why I prefer to use simply a for loop. – Yves Jan 07 '22 at 07:17
  • 1
    This code needs a check, like this : https://onlinegdb.com/09tQKU6Kn, because this approach will fail for empty strings. (Also try to avoid using : using namespace std;) – Pepijn Kramer Jan 07 '22 at 07:23
  • 1
    1) `::tolower` should be `std::lower`. This is C++, not C. 2) `std::tolower` should *not* be passed to `std::transform` directly, see an explanation [here](https://en.cppreference.com/w/cpp/string/byte/tolower#Notes). 3) [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Evg Jan 07 '22 at 07:43