Excerpt
This article explains different techniques like toupper(), transform algorithms, loops, and stringstreams to change lowercase letters to uppercase in C++ strings.
C++ provides several straightforward ways to convert lowercase characters in a string to uppercase. This includes built-in functions like toupper(), algorithms like transform(), and stringstreams.
Let’s explore some common techniques for changing lowercase to uppercase in C++ code.
Introduction
When working with string data in C++, you may need to standardize the case by converting lowercase letters to uppercase. Here are some approaches:
- toupper() function - Simple uppercase conversion
- transform algorithm - Apply toupper() to entire strings
- for loops - Manual iteration and transformation
- stringstreams - Set uppercase format flag
- online tools - Quick case conversion
We’ll walk through code examples of each method for changing case in C++ strings.
toupper() Function
The toupper() function in the
1#include <iostream>
2#include <cctype>
3
4int main() {
5
6 char lower = 'a';
7 char upper = toupper(lower);
8
9 std::cout << upper << std::endl; // 'A'
10
11 return 0;
12}
toupper() takes a char as input and returns its uppercase form.
transform Algorithm
To uppercase an entire string, we can use the transform algorithm from
1#include <iostream>
2#include <algorithm>
3#include <cctype>
4
5int main() {
6
7 std::string lower = "hello world";
8
9 transform(lower.begin(), lower.end(), lower.begin(), ::toupper);
10
11 std::cout << lower << std::endl; // "HELLO WORLD"
12
13 return 0;
14}
transform applies toupper() to each character of the string in place.
for Loops
For more manual control, iterate through the string yourself with a for loop:
1#include <iostream>
2
3int main() {
4
5 std::string lower = "hello world";
6
7 for(int i=0; i<lower.length(); i++) {
8 lower[i] = toupper(lower[i]);
9 }
10
11 std::cout << lower << std::endl; // "HELLO WORLD"
12
13 return 0;
14}
This loops through each character and uppercases it.
Free Online Case Conversion Tools
For quick case conversion, sites like String to Uppercase are handy.
Paste your C++ string and get the uppercase version without any coding!
String Streams
C++ string streams can be configured to output strings in uppercase:
1#include <iostream>
2#include <sstream>
3
4int main() {
5
6 std::string lower = "hello world";
7
8 std::stringstream ss;
9 ss << std::uppercase << lower;
10
11 std::string upper = ss.str();
12
13 std::cout << upper << std::endl; // "HELLO WORLD"
14
15 return 0;
16}
The uppercase flag formats the inserted string as uppercase.
Use Cases
Some common use cases for uppercase conversion in C++ include:
- Sanitizing user input into a standard format
- Defining constants in uppercase by convention
- Performing case-insensitive string comparisons
Uppercase strings have many applications across C++ programs.
Conclusion
In C++, transforming lowercase strings to uppercase can be done easily using:
- toupper() for single characters
- transform with toupper() for full strings
- for loops to iterate through each character
- stringstreams configured for uppercase
- online conversion tools
The toupper() function and transform algorithm provide the most direct way to change case in C++.
Converting strings to uppercase or lowercase is a useful technique for string processing and comparison in C++.