Open In App
Related Articles

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

Improve Article
Improve
Save Article
Save
Like Article
Like

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




// 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

Time complexity: O(N) where N is length of string ,as to transform string to Upper/Lower we have to traverse through all letter of string once.
Auxiliary Space: O(1) 

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.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 10 Jan, 2023
Like Article
Save Article
Similar Reads