Skip to content
Related Articles

Related Articles

Conversion of whole String to uppercase or lowercase using STL in C++

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 14 Sep, 2022
Improve Article
Save Article

Given a string, convert the whole string to uppercase or lowercase using STL in C++.

Examples:

For uppercase conversion
Input: s = “String”
Output: s = “STRING”

For lowercase conversion
Input: s = “String”
Output: s = “string”

Functions used : transform : Performs a transformation on given array/string. toupper(char c): Returns the upper case version of character c. If c is already in uppercase, return c itself. tolower(char c) : Returns lower case version of character c. If c is already in lowercase, return c itself.

 

CPP-STL-Self-Paced-Course 

CPP




// C++ program to convert whole string to
// uppercase or lowercase using STL.
 
#include<bits/stdc++.h>
using namespace std;
 
int main(){
    // s1 is the string which is converted to uppercase
    string s1 = "abcde";
 
    // using transform() function and ::toupper in STL
    transform(s1.begin(), s1.end(), s1.begin(), ::toupper);
    cout<<s1<<endl;
 
    // s2 is the string which is converted to lowercase
    string s2 = "WXYZ";
 
    // using transform() function and ::tolower in STL
    transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
   cout<<s2<<endl;
 
    return 0;
}

Output

ABCDE
wxyz

This article is contributed by Jatin Goyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!