Example 1: Convert Entire String to Lowercase
#include <iostream>
#include <algorithm>
int main() {
std::string input = “HelloWorld”;
std::transform(input.begin(), input.end(), input.begin(), ::tolower);
std::cout << “Lowercase string: ” << input << std::endl;
return 0;
}
Explanation: This example demonstrates the use of std::transform
to convert the entire string to lowercase. The ::tolower
function is applied to each character in the string, modifying it in place.
Example 2: Convert First Letter of Each Word to Lowercase
#include <iostream>
#include <sstream>
int main() {
std::string sentence = “The Quick Brown Fox”;
std::istringstream iss(sentence);
std::ostringstream result;
do {
std::string word;
iss >> word;
std::transform(word.begin(), word.end(), word.begin(), ::tolower);
result << word << ” “;
} while (iss);
std::cout << “Title case to Lowercase: ” << result.str() << std::endl;
return 0;
}
Explanation: Here, we use std::istringstream
to tokenize the input sentence into words. Then, std::transform
is applied to convert each word’s first letter to lowercase.