Transforming a String to Uppercase Letters

Transforming a String to Uppercase Letters

To change the case of all the letters in a string object, use the std::transform() algorithm. This algorithm is defined in the standard header . It takes four arguments. The first two arguments are input iterators that mark the beginning and the end of the string, respectively. The third argument is an output iterator which points to the beginning of the original string (the uppercase version of the string is written the original string). The fourth argument is a unary operator (i.e., an object that supports the () operator and takes one argument). The trick is that transform() applies the unary operator to manipulate every element in the sequence. This unary operator can be a pointer to a function or an object function. In our example, we pass the address of the standard function toupper() that is defined in the standard header :

 #include  #include  #include  #include   // for toupper()int main(){ std::string s("hello"); // lowercase // transform the string to uppercase std::transform(s.begin(), // original string's beginning                s.end(), // original string's end                s.begin(), // where to write the new string?                toupper); // unary operator std::cout << s; // display "HELLO"}

Alas, the program above will not compile because the name 'toupper' is ambiguous: there are two functions with this name:

 1) int std::toupper(int); // from 2) template    charT std::toupper(charT, const locale&); // from 

We want the first function to be called. To resolve the ambiguity, we add an explicit cast:

 std::transform(s.begin(), s.end(), s2.begin(),                static_cast < int(*)(int) > (toupper));

This tells the compiler which version of toupper() to choose, namely the one declared in . In a similar vein, you can transform a string to lowercase letters by using the tolower() function.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as