In this article, we will discuss different ways to convert a string to uppercase in C++.
Table Of Contents
Method 1: Using std::toupper() & for_each()
Using STL Algorithm for_each(), we can iterate over each character of string. During iteration, for_each() function will pass the refrence of each character to the given lambda function, which intern changes the case of character to upper case. Therefore, at the end of loop, all characters in string will be in uppercase.
Let’s see an example,
#include <iostream> #include <string> #include <algorithm> #include <cctype> int main() { std::string sampleText = "This is a sample text"; // Iterate over each character in string // and convert each character to upper case std::for_each( sampleText.begin(), sampleText.end(), [](char& ch){ ch = std::toupper(ch); }); std::cout << sampleText << std::endl; return 0; }
Output:
THIS IS A SAMPLE TEXT
It converted the string to uppercase.
Method 2: Using transform() & toupper()
Using STL Algorithm transform(), we can transform over each character of string. Pass the iterators pointing to start and end of string, as first two argument in the transform() function. Then pass the iterator pointing to the start of string, as third argument i.e. output range. Also, pass a function ::toupper() as fourth argument in the transform() function.
Frequently Asked:
This transform() function will iterate over each character of string, pass each character to the toupper() function. Which intern returns the uppercase version of character. Then transform() function will save it to the string again. This way all characters will become uppercase in the string.
Let’s see an example,
#include <iostream> #include <string> #include <algorithm> #include <cctype> int main() { std::string sampleText = "This is a sample text"; // Iterate over each character in string // and transform each character to upper case std::transform( sampleText.begin(), sampleText.end(), sampleText.begin(), ::toupper); std::cout << sampleText << std::endl; return 0; }
Output:
THIS IS A SAMPLE TEXT
Output:
THIS IS A SAMPLE TEXT
It converted the string to uppercase.
Method 3: Using transform() & Lambda Function
Using STL Algorithm transform(), we can transform over each character of string. Pass the iterators pointing to start and end of string, as first two argument in the transform() function. Then pass the iterator pointing to the start of string, as third argument i.e. output range. Also, pass a lambda function as fourth argument in the transform() function.
This transform() function will iterate over each character of string, and pass each character to the given lambda function. Which intern returns the uppercase version of character. Then transform() function will save it to the string again. This way all characters will become uppercase in the string.
Let’s see an example,
#include <iostream> #include <string> #include <algorithm> #include <cctype> int main() { std::string sampleText = "This is a sample text"; // Iterate over each character in string // and transform each character to upper case std::transform( sampleText.begin(), sampleText.end(), sampleText.begin(), [](const char& ch){ return ::toupper(ch); }); std::cout << sampleText << std::endl; return 0; }
Output:
THIS IS A SAMPLE TEXT
Output:
THIS IS A SAMPLE TEXT
It converted the string to uppercase.
Method 4: Using for loop
It is the basic solution. Just iterate over all characters of string using a for loop, and convert each character to uppercase using ::toupper(). Let’s see an example,
#include <iostream> #include <string> #include <algorithm> #include <cctype> int main() { std::string sampleText = "This is a sample text"; // Iterate over each character in string // and change each character to upper case for(char& ch: sampleText) { ch = ::toupper(ch); } std::cout << sampleText << std::endl; return 0; }
Output:
Pointers in C/C++ [Full Course]
THIS IS A SAMPLE TEXT
It converted the string to uppercase.
Summary
We learned about different ways to convert a string to uppercase in C++. Thanks.