
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert C++ String to Upper Case
In C++, converting a string into uppercase means changing all the characters into uppercase. This is a common requirement in many applications, such as formatting output, standard data input, and case sensitivity.
Following is the table that shows the conversion of string to uppercase:
String Case Characters | Uppercase Characters |
---|---|
TutORix | TUTORIX |
TUTORIALSPOINT | tutorialspoint |
- Using transform() function
- Using loop and toupper()
- Using for loop
- Using C-style string with toupper()
Uppercase Conversion Using transform() Function
The transform() is a simple C++ STL function that allows the user to perform a function for each value in the dataframe.
Syntax
Its syntax is as follows:
transform(str.begin(), str.end(), str.begin(), ::toupper);
Here,
- str.begin(), str.end(): Input range.
- str.begin(): It overwrite the original string.
- ::toupper: This is C function from the header <cctype> that uppercases each char.
Example
In this example, we use tranform() function to convert string to uppercase.
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string str = "hello world"; transform(str.begin(), str.end(), str.begin(), ::toupper); cout << str; return 0; }
Output
The above program obtained the following result:
HELLO WORLD
Uppercase Conversion Using for Loop and toupper()
In this method, we manually iterate over the string and apply toupper() to each character.
Example
In this program, we iterate the characters through the string length() function and get the result in uppercase format.
#include <iostream> #include <string> #include <cctype> using namespace std; int main() { string str = "TUTORIALSPOINT"; for (int i = 0; i < str.length(); ++i) { str[i] = toupper(str[i]); } cout << str; return 0; }
Output
The above program obtained the following result:
tutorialspoint
Uppercase Conversion Using R-based for Loop and toupper()
By using a C++ range-based for loop, convert any string type into uppercase.
Example
The example of the for loop is to set the range of string characters to convert into uppercase.
#include <iostream> #include <string> #include <cctype> using namespace std; int main() { string str = "TUTORIX"; for (char &ch : str) { ch = toupper(ch); } cout << str; return 0; }
Output
The above program obtained the following result:
tutorix
Uppercase Conversion Using C-style String with toupper()
Suppose you are using a C-style character array; you can still use toupper() in a loop that changes the string into uppercase.
Example
In this program, we execute toupper() function to implement the conversion of string into uppercase.
#include <iostream> #include <cctype> #include <cstring> using namespace std; int main() { char str[] = "c style string"; for (int i = 0; str[i]; i++) { str[i] = toupper(str[i]); } cout << str; return 0; }
Output
The above program obtained the following result:
C STYLE STRING