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
How to Reverse a String in C++? Reversing a string means replacing the first character with the last character, second character with the second last character and so on. In this article, we will learn how to reverse a string in C++.ExamplesInput: str = "Hello World"Output: dlroW olleHExplanation: The last character is replaced by
2 min read
Convert character array to string in C++ This article shows how to convert a character array to a string in C++. The std::string in c++ has a lot of inbuilt functions which makes implementation much easier than handling a character array. Hence, it would often be easier to work if we convert a character array to string. Examples: Input: ch
4 min read
Convert Camel Case String to Snake Case in C++ Programming can be complicated, and it's essential to ensure that our code is readable and well-structured. One way to improve code clarity is by using meaningful and descriptive variable and function names. To make every element name unique and more meaningful we use camel case and snake case are t
3 min read
How Do I Iterate Over the Words of a String? In C++, strings are character sequences stored in a char array. It may contain text in the form of multiple words representing a sentence. In this article, we will learn how we can iterate over the words of a string in C++. Example: Input: myString ="Geek for Geeks" Output: Geek for GeeksIterate Ove
2 min read
Take String as Input in C++ Strings are used to store the textual information. Taking a string as input is a very common operation used in almost all fields of programming. In this article, we will look at different ways to take string as input.The simplest way to take string as the user input is to use cin object. Let's take
2 min read