How to convert an instance of std string to lower case

0 votes
I'd want to change a std::string to lowercase.

I am aware of the lower function ().

However, I've had problems with this method in the past, and using it with a std::string would require iterating over each character.

Is there an option that works 100% of the time?
Aug 2 in C++ by Nicholas
• 6,240 points
70 views

1 answer to this question.

0 votes
#include <algorithm>
#include <cctype>
#include <string>

std::string data = "Abc";
std::transform(data.begin(), data.end(), data.begin(),
    [](unsigned char c){ return std::tolower(c); });

You're not going to get away with not going over each character. 

Otherwise, there is no way to tell whether the character is lowercase or uppercase.

If you despise tolower(), here's a customised ASCII-only solution I don't recommend:

char asciitolower(char in) {
    if (in <= 'Z' && in >= 'A')
        return in - ('Z' - 'z');
    return in;
}

std::transform(data.begin(), data.end(), data.begin(), asciitolower);

Tolower() can only do single-byte character substitutions, which is inconvenient for many scripts, notably those that use a multi-byte encoding like UTF-8.

answered Aug 2 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

How to initialize an std::string with a length?

How can I correctly initialise a string if its length is specified at build time? #include <string> int length = 3; string word[length]; //invalid ...READ MORE

Jul 27 in C++ by Nicholas
• 6,240 points
47 views
0 votes
1 answer

How to use std::sort to sort an array in C++

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays: #include <algorithm> int main(){ int v[2000]; ...READ MORE

answered Jun 1 in C++ by Damon
• 4,960 points
77 views
0 votes
1 answer

How to convert string to char array in C++?

Simplest way I can think of doing ...READ MORE

answered Jun 20 in C++ by Damon
• 4,960 points
384 views
0 votes
0 answers

How to convert a char array to a string?

Converting a C++ string to a char ...READ MORE

Jul 14 in C++ by Nicholas
• 6,240 points
54 views
0 votes
1 answer

Easiest way to convert int to string in C++

The C++11 version has introduced the std::stoi ...READ MORE

answered Feb 10 in Others by Rahul
• 9,240 points
53 views
0 votes
1 answer

Easiest way to convert int to string in C++

C++ adds std::stoi (and variants for each numeric type) and std::to string, which are the C equivalents of atoi and itoa but expressed in terms of std::string #include <string> std::string s = std::to_string(42); Is therefore ...READ MORE

answered Jun 1 in C++ by Damon
• 4,960 points
176 views
0 votes
1 answer

Using getline() in C++

If you use getline() after cin >> anything, you must first flush the newline character from the buffer.  You can achieve this by using the cin.ignore() It would be something like this: string messageVar; cout ...READ MORE

answered Jun 1 in C++ by Damon
• 4,960 points
121 views
0 votes
1 answer

Easiest way to convert int to string in C++

std::stoi (and variations for each numeric type) ...READ MORE

answered Jun 15 in C++ by Damon
• 4,960 points
104 views
0 votes
1 answer

How can I convert a std::string to int?

There are some new convert methods in C++ that convert std::string to a numeric type. As an alternative to str.c str() atoi(str.c str()) atoi(str.c str() you can make use of std::stoi std::stoi ...READ MORE

answered Jun 1 in C++ by Damon
• 4,960 points
52 views
0 votes
1 answer

How to reverse an std::string?

A reverse function is integrated into C++ and can be used to reverse a string.  This function accepts two parameters: The start iterator for the string The string iterator has come to an end. The following line of code demonstrates how to use this function: #include <iostream> //The library below must be included ...READ MORE

answered Jun 1 in C++ by Damon
• 4,960 points
49 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP