他の多くの回答がすでに述べているように、 への引数std::toupper
が渡され、結果が値によって返されますstd::toupper('a')
。'a'
リテラルをインプレースで変更することはできません。また、読み取り専用バッファーに入力があり、大文字の出力を別のバッファーに格納したい場合もあります。したがって、値によるアプローチははるかに柔軟です。
一方、冗長なのは とのチェックisalpha
ですislower
。文字が小文字のアルファベット文字でない場合は、そのtoupper
ままにしておくため、ロジックはこれに縮小されます。
#include <cctype>
#include <iostream>
int
main()
{
char text[] = "Please send me 400 $ worth of dark chocolate by Wednesday!";
for (auto s = text; *s != '\0'; ++s)
*s = std::toupper(*s);
std::cout << text << '\n';
}
これがよりきれいである場合は、アルゴリズムを使用して生のループをさらに排除できます。
#include <algorithm>
#include <cctype>
#include <iostream>
#include <utility>
int
main()
{
char text[] = "Please send me 400 $ worth of dark chocolate by Wednesday!";
std::transform(std::cbegin(text), std::cend(text), std::begin(text),
[](auto c){ return std::toupper(c); });
std::cout << text << '\n';
}