Conversion of whole String to uppercase or lowercase using STL in C++
Last Updated :
10 Jan, 2023
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...