In this C++ Program Tutorial we will explore how to convert a C++ String from lowercase to uppercase using a variety of different methods available to us.
See Also: Converting uppercase strings/chars to lowercase
Lowercase to Uppercase – Method#1
At the heart of it all, a character is merely an integer value representing a character from the ASCII table. If one were to study the ASCII table, you will observe a pattern between the Uppercase and Lowercase variants of the characters.
If we subtract 32 from the integer value of any Lowercase character, we will obtain it’s uppercase variant. Subtracting 32 from lowercase will give the same effect in reverse.
#include <iostream>
#include <string>
using namespace std;
int main() {
char a = 'a';
char b = 'b';
char c = 'c';
cout << a - 32 << endl;
cout << b - 32 << endl;
cout << c - 32 << endl;
}
The ASCII values of the uppercase versions of the a, b and c characters.
65
66
67
If you want to print out the values in the character versions, you can either use typecasting as shown in the example below, are store the updated integer (ascii) value into a char variable, and then print it out.
#include <iostream>
#include <string>
using namespace std;
int main() {
char a = 'a';
char b = 'b';
char c = 'c';
cout << (char) (a - 32) << endl;
cout << (char) (b - 32) << endl;
cout << (char) (c - 32) << endl;
}
The output:
A
B
C
By simply iterating over an entire string, and subtracting 32 from all characters from a – z, we can convert any Lowercase string to Uppercase.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "hello world";
for (int i = 0; i < str.length(); i++) {
str[i] = str[i] - 32;
cout << str[i];
}
}
The new output string:
HELLO WORLD
Method#2 – toupper() function
A newer and better alternative for converting Lowercase Strings to Uppercase is the toupper() function.
In this example, we will take a look at how to convert some simple characters in C++ to uppercase. This function will work on both uppercase and lowercase, but it just won’t have any effect on uppercase.
int main() {
char a = 'a';
char b = 'b';
char c = 'c';
cout << toupper(a) << endl;
cout << toupper(b) << endl;
cout << toupper(c) << endl;
}
The output:
65
66
67
If you want the output to be in the form of actual characters, you can use typecasting, and convert the int value to its’ character representation.
int main() {
string var = "Hello World";
char a = 'a';
char b = 'b';
char c = 'c';
cout << (char) toupper(a) << endl;
cout << (char) toupper(b) << endl;
cout << (char) toupper(c) << endl;
}
The output:
A
B
C
Alternatively, you can just store the int value returned into a char variable, which will print out an actual character if you try printing it.
In this example we will take a look at how to convert a String to Uppercase. The toupper() function does not work on strings natively, but remember that a String is merely a collection of characters. Hence, with the following approach, where iterate over each individual character in a String, we can convert it to uppercase.
#include <iostream>
#include <string>
using namespace std;
int main() {
string var = "hello world";
for (int i = 0; i < var.length(); i++) {
var[i] = toupper(var[i]);
}
cout << "New String: " << var << endl;
}
The output:
New String: HELLO WORLD
This marks the end of the C++ Program to Convert String Lowercase to Uppercase Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content will be asked in the comments section below.