How to Convert a C++ String to Uppercase?
Last Updated :
17 Oct, 2024
Converting a string to uppercase means changing each character of the string that is in lowercase to an uppercase character. In the article, we will discuss how to convert a string to uppercase in C++.
Examples
Input: str = "Geeksforgeeks"
Output: GEEKSFORGEEKS
Explanation: Every character of string converted to uppercase.
Input: str = "Hello"
Output: HELLO
Explanation: Every character of string converted to uppercase.
Following are the different ways to convert the string to uppercase in C++:
We can use std::transform() with toupper() function convert the string to uppercase. The std::transform() transforms the string by applying the given function to each character.
Syntax
std::transform(first, last, pos, toupper());
where first and last are the iterator to the beginning and the end of the string and pos the starting position of the destination.
Example
C++
// C++ program to convert a string to uppercase
// using std::transform()
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "Geeksforgeeks";
// Using transform with toupper to convert
// string to uppercase
transform(s.begin(), s.end(), s.begin(),
::toupper);
cout << s;
return 0;
}
Time Complexity: O(n), where n is the number of characters in the string.
Auxiliary Space: O(1)
In this case, we have to use the scope resolution operator (::) to refer to the global toupper() function.
Using std::toupper() With Loop
In the above method, we can also use loop instead of std::stransform() function with toupper(). This loop will manually apply the toupper() function to each character of the string.
Example
C++
// C++ program to convert a string to uppercase
// uisng toupper() with loop
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "Geeksforgeeks";
// Convert every character of string to
// uppercase using toupper() method
for (int i = 0; i < s.length(); i++)
s[i] = toupper(s[i]);
cout << s;
return 0;
}
Time Complexity: O(n), where n is the number of characters is string.
Auxiliary Space: O(1)
Manually Using ASCII Value Manipulation
In C++, lowercase and uppercase alphabets differ by a fixed value in the ASCII table. So, we can convert lowercase characters to uppercase by subtracting 32 from their ASCII value. The characters already in uppercase have to be handled separately either by using std::isupper() function or checking whether the ASCII value of character lies withing the range of uppercase characters.
Example
C++
// C++ program to convert a string to uppercase
// using ASCII Value Manipulation
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "Geeksforgeeks";
// Convert the string to uppercase using
// ASCII values
for (int i = 0; i < s.length(); i++) {
if (s[i] >= 'a' && s[i] <= 'z')
s[i] = s[i] - 32;
}
cout << s;
return 0;
}
Time Complexity: O(n), where n is the number of characters in the string.
Auxiliary Space: O(1)
Similar Reads
How to Convert a std::string to char* in C++? In C++, strings are the textual data that is represented in two ways: std::string which is an object, and char* is a pointer to a character. In this article, we will learn how to convert a std::string to char* in C++. Example Input:string myString = "GeeksforGeeks";Output:char * myString = "Geeksfor
1 min read
How to Convert std::string to Lower Case? Converting string to lower case means all the alphabets present in the string should be in lower case. In this article, we will learn how to convert the std::string to lowercase in C++.Examples Input: s = "GEEKS for GEEKS"Output: geeks for geeksExplanation: All characters of s are converted to lower
3 min read
How to Convert a Single Character to String in C++? A string is a generally made up of a sequence of multiple characters. In this article, we will learn how to convert a single character to a string in C++.Example:Input: c = 'A'Output: s = "a"Explanation: Character c is converted to a string.Input: c = '#'Output: s = "#"Explanation: Character c is co
4 min read
Convert String to Char Array in C++ In C++, we usually represent text data using the std::string object. But in some cases, we may need to convert a std::string to a character array, the traditional C-style strings. In this article, we will learn how to convert the string to char array in C++.ExamplesInput: str = "geeksforgeeks"Output
4 min read
Convert char* to std::string in C++ Strings are generally represented as an instance of std::string class in C++. But the language also supports the older C-Style representation where they are represented as array of characters (char* or char[]) terminated by null character '\0'. In this article, we will learn how to convert the char*
3 min read