Home >Backend Development >C++ >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!