transform
toupper
tolower
场景:
1. 转换字符串大小写,只有C的toupper怎么办???
代码:
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
int main(int argc, char const *argv[])
{
string str("abcdefghijklnmopqrstuvwxyz123");
std::transform(str.begin(),str.end(),str.begin(),::toupper);
cout << str << endl;
char str2[] = "abcdefghijklnmopqrstuvwxyz1234";
std::transform(str2,str2+26,str2,::toupper);
cout << str2 << endl;
return 0;
}输出:
ABCDEFGHIJKLNMOPQRSTUVWXYZ123
ABCDEFGHIJKLNMOPQRSTUVWXYZ1234
C/C++标准库中的字符串大小写转换

本文介绍了在C++中如何使用标准库函数`transform`、`toupper`和`tolower`来实现字符串的大小写转换。针对仅熟悉C语言中`toupper`的情况,提供了C++的解决方案。
827

被折叠的 条评论
为什么被折叠?



