c++国家排序大小写互换
时间: 2023-11-24 13:52:17 浏览: 22
以下是C++代码,可以实现输入字符串,将其中所有的大写字母改为小写字母,而所有小写字母改为大写字母,其它字符不变,并按照字典序排序输出。
```c++
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
bool cmp(char a, char b) {
if (tolower(a) == tolower(b)) {
return a < b;
}
return tolower(a) < tolower(b);
}
int main() {
string s;
cin >> s;
sort(s.begin(), s.end(), cmp);
cout << s << endl;
return 0;
}
```
相关问题
c++选择排序由小到大
以下是C++选择排序由小到大的代码实现:
```cpp
#include <iostream>
using namespace std;
int main() {
int n, i, j, minIndex, temp;
int a[2000];
cin >> n;
for (i = 0; i < n; i++) {
cin >> a[i];
}
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (a[j] < a[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
}
for (i = 0; i < n; i++) {
cout << a[i] << " ";
}
return 0;
}
```
c++string类型大小写转换
可以使用C++标准库中的`std::transform`函数来实现字符串大小写转换。具体实现如下:
```c++
#include <algorithm>
#include <string>
std::string str = "Hello World!";
std::transform(str.begin(), str.end(), str.begin(), ::tolower); // 转换为小写
std::transform(str.begin(), str.end(), str.begin(), ::toupper); // 转换为大写
```
其中,`::tolower`和`::toupper`是C++标准库中的函数,用于将字符转换为小写或大写。