{"title":"C++ 字符串小写转大写 - 保持其他字符不变","description":"本文介绍如何使用 C++ 中的 toupper() 函数将字符串中的小写字母转换为大写字母,同时保持其他字符不变。提供示例代码和运行结果。","keywords":"C++, 字符串, 小写, 大写, toupper, 转换, 代码, 示例, 运行结果","content":"要将一个字符串中的小写字母转换为大写字母,可以使用C++中的toupper()函数。toupper()函数可以将一个字符转换为大写形式,如果该字符不是小写字母,则保持不变。\n\n下面是一个示例代码:\n\nc++\n#include <iostream>\n#include <cctype> // 包含toupper()函数的头文件\nusing namespace std;\n\nint main() {\n string str = "Hello World!"; // 原始字符串\n string result = ""; // 转换后的字符串\n\n for (char c : str) {\n if (islower(c)) { // 如果是小写字母\n result += toupper(c); // 转换为大写字母并添加到结果字符串\n } else {\n result += c; // 其他字符不变,直接添加到结果字符串\n } \n }\n\n cout << result << endl; // 输出转换后的字符串\n\n return 0;\n}\n\n\n运行结果为:HELLO WORLD!"}


原文地址: https://www.cveoy.top/t/topic/qbrL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录