I want to convert a std::string
to lowercase. I am aware of the function tolower()
, however in the past I have had issues with this function and it is hardly ideal anyway as use with a std::string
would require iterating over each character.
Is there an alternative which works 100% of the time?
Using range-based for loop of C++11 a simpler code would be :
Code Snippet
This could be another simple version to convert uppercase to lowercase and vice versa. I used VS2017 community version to compile this source code.
Note: if there are special characters then need to be handled using condition check.
There is a Boost string algorithm for this:
Or, for non-in-place:
Here's a macro technique if you want something simple:
However, note that @AndreasSpindler's comment on this answer still is an important consideration, however, if you're working on something that isn't just ASCII characters.
For more information: http://www.cplusplus.com/reference/locale/tolower/