Home > Backend Development > C++ > How to Convert a String to Uppercase in C ?

How to Convert a String to Uppercase in C ?

Linda Hamilton
Release: 2024-12-27 11:03:11
Original
184 people have browsed it

How to Convert a String to Uppercase in C  ?

Converting a String to Upper Case in C

Within the vast realm of programming, string manipulation holds immense importance. Among the myriad string manipulation tasks, the conversion of strings to upper case is a common requirement.

How to Convert a String to Upper Case

In the context of C , the and headers hold the key to string conversion. Consider the following code snippet:

#include <algorithm>
#include <string>

std::string str = "Hello World";
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
Copy after login

Breaking Down the Code

  • The header provides access to the std::string class, which represents strings in C .
  • The header defines the std::transform algorithm, which applies a transformation to each element in a range.

In this specific instance, the code performs the following steps:

  1. Initializes a string str with the value "Hello World."
  2. Calls std::transform and specifies three essential arguments:

    • str.begin() and str.end(): The range of elements to be transformed (the entire str)
    • str.begin(): The start of the output range (overwritten with the transformed values)
    • ::toupper: A function pointer that transforms each character to upper case (thanks to the double colons)

The transformation is performed in-place, effectively converting the characters within the str string to upper case.

Output

Assuming successful execution, the str string now holds the value "HELLO WORLD."

The above is the detailed content of How to Convert a String to Uppercase in C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template