c++编写一个函数,接受一个字符串,将其中的大写字母转换为小写,小写字母转换为大写。
作者:野牛程序员:2023-08-11 20:31:50 C++阅读 2704
c++编写一个函数,接受一个字符串,将其中的大写字母转换为小写,小写字母转换为大写。
#include <iostream>
#include <string>
#include <cctype>
std::string swapCase(const std::string& str) {
std::string result = str;
for (char& c : result) {
if (std::isupper(c)) {
c = std::tolower(c);
} else if (std::islower(c)) {
c = std::toupper(c);
}
}
return result;
}
int main() {
std::string text = "Hello, World!";
std::string swapped = swapCase(text);
std::cout << "Swapped case: " << swapped << std::endl;
return 0;
}野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

