C++ std::toupper
Normalize ASCII identifier bytes using the C character-conversion function in the active locale.
A simple example
std::toupper
- Start with "ab"
- Do this Convert to uppercase
- Result "AB"
Syntax
std::toupper
Run these examples in C++
Save one example at a time as lesson.cpp. Use C++17 and a Unix-style shell.
c++ -std=c++17 -Wall -Wextra lesson.cpp -o lesson
./lessonNormalize a campus code
A school imports a short campus code from a form with inconsistent capitalization. Produce the standard spelling before joining the form record to the campus directory.
#include <cctype>
#include <iostream>
#include <string>
int main() {
std::string code = "bio";
for (char &unit : code)
unit = static_cast<char>(std::toupper(static_cast<unsigned char>(unit)));
std::cout << code << "\n";
return 0;
}
BIO
The input contains ASCII. Converting each byte to unsigned char makes it valid for the character-classification API.
Normalize an airport tag
Two terminals can enter the same short label with different capitalization or padding. Normalize the incoming spelling before lookup so the stored identifiers can follow a consistent convention.
#include <cctype>
#include <iostream>
#include <string>
int main() {
std::string code = "maa-2";
for (char &unit : code)
unit = static_cast<char>(std::toupper(static_cast<unsigned char>(unit)));
std::cout << code << "\n";
return 0;
}
MAA-2
The input contains ASCII. Converting each byte to unsigned char makes it valid for the character-classification API.
Print a workshop badge
A student types a lower-case room assignment. Convert the letter part to uppercase for the printed label. Digits and the separator retain their spelling.
What to watch for
For UTF-8 human names choose a Unicode-aware casing library. These examples run with the default C locale.