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
#include<bits/stdc++.h>
using namespace std;
int main(){
string s1 = "abcde" ;
transform(s1.begin(), s1.end(), s1.begin(), :: toupper );
cout<<s1<<endl;
string s2 = "WXYZ" ;
transform(s2.begin(), s2.end(), s2.begin(), :: tolower );
cout<<s2<<endl;
return 0;
}
|
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!