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.
// 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;
} |
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)