c++小写字符串转大写字符串
时间: 2023-08-12 21:09:38 浏览: 252
你可以使用C++的标准库函数std::toupper
来将小写字符串转换为大写字符串。下面是一个示例代码:
#include <iostream>
#include <cctype> // 包含toupper函数的头文件
int main() {
std::string str = "hello world";
for (char& c : str) {
c = std::toupper(c); // 将每个字符转换为大写
}
std::cout << str << std::endl; // 输出大写字符串
return 0;
}
这段代码将字符串"hello world"转换为全大写字符串"HELLO WORLD"并输出。你可以根据自己的需求修改代码中的字符串。
相关问题
c++string字符串大写转小写
可以通过循环遍历字符串并调用 tolower()
函数来实现将大写字母转换为小写字母,例如:
#include <iostream>
#include <string>
int main() {
std::string s = "HELLO WORLD!";
for (char& c : s) {
c = tolower(c);
}
std::cout << s << std::endl; // 输出 "hello world!"
return 0;
}
在循环中,将字符串中的每个字符都转换为小写字母,并将其重新赋值给原来的位置。最后输出转换后的字符串即可。
c++字符串大写转小写
在C++中,将字符串从大写转换为小写可以使用标准库提供的std::tolower
函数,这个函数通常与ctype
库一起使用。如果你有一个std::string
类型的变量,你可以遍历每个字符并应用tolower
。下面是一个简单的示例:
#include <cctype> // 包含tolower函数
#include <string>
std::string toLowerCase(std::string str) {
for (char& c : str) { // 遍历字符串中的每个字符
if (isupper(c)) { // 检查是否为大写字母
c = std::tolower(c); // 转换为小写
}
}
return str;
}
然后,你可以像这样使用这个函数:
std::string input = "HELLO WORLD";
input = toLowerCase(input);
std::cout << input; // 输出 "hello world"
相关推荐
















