我有一个字符串,它可以包含数字以及大小写字母。我需要将所有的大写字母转换为小写字母,反之亦然。我们该怎么做呢?
发布于 2010-11-03 13:50:08
迭代字符串并使用isupper()确定每个字符是否为大写。如果它是大写的,使用tolower()将其转换为小写。如果不是大写,使用toupper()将其转换为大写。
发布于 2010-11-03 13:52:29
这里有一个不用boost就能做到的方法:
#include <string>
#include <algorithm>
#include <cctype>
#include <iostream>
char change_case (char c) {
if (std::isupper(c))
return std::tolower(c);
else
return std::toupper(c);
}
int main() {
std::string str;
str = "hEllo world";
std::transform(str.begin(), str.end(), str.begin(), change_case);
std::cout << str;
return 0;
}发布于 2010-11-03 13:43:17
您可以遍历字符串并从每个字母字符中添加或减去适当的数字,以便将ASCII值解析为大小写相反的ASCII值。
https://stackoverflow.com/questions/4084458
复制相似问题