string to lowercase c++ code example

Example 1: string to upper c++

std::string data = "This is a sample string.";
// convert string to upper case
std::for_each(data.begin(), data.end(), [](char & c){
c = ::toupper(c);
});

Example 2: tolower funciton in cpp

transform(s.begin(), s.end(), s.begin(), ::tolower);

Example 3: tolower in c++

#include <cctype>
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

int main()
{
    char str[] = "John is from USA.";

    cout << "The lowercase version of \"" << str << "\" is " << endl;

    for (int i=0; i<strlen(str); i++)
        putchar(tolower(str[i]));
    
    return 0;
}

Example 4: convert whole string to lowercase c++

#include<bits/stdc++.h> 
using namespace std; 
main() { 
    string s = "Viet Nam"; 
    transform(s.begin(), s.end(), s.begin(), ::tolower); //lowercase
    cout << s << endl; 
}

Example 5: convert all strings in vector to lowercase or uppercase c++

for(std::string &s : stringVector){
    std::transform(s.begin(), s.end(), s.begin(), 
        [](char c){ return std::toupper(c); }); // for lowercase change "toupper" -> "tolower" 
}

Example 6: c++ string functions lowercase

transform(su.begin(), su.end(), su.begin(), ::toupper);
transform(sl.begin(), sl.end(), sl.begin(), ::tolower);

Tags:

Java Example