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

How Can I Convert a String to Uppercase in C ?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-22 13:50:101022browse

How Can I Convert a String to Uppercase in C  ?

Converting a String to Upper Case in C

Converting a string to upper case is a common task in programming. C provides built-in functions that simplify this process.

The standard library function std::transform() can be used with a lambda or function pointer to modify each character in a string. To convert a string to upper case, we can use ::toupper as the transformation function:

#include <algorithm>
#include <string>

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

This code creates a string str with the value "Hello World." The std::transform() function then iterates over each character in str and applies the ::toupper function to it. ::toupper converts each character to its upper case equivalent, resulting in the string "HELLO WORLD."

The std::transform() function is versatile and can be used to apply any transformation to a string, making it a powerful tool for string manipulation in C .

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

Statement:
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