C++对string进行大小写转换操作方法
作者:YAIMZA 时间:2023-11-03 04:32:59
C++对string进行大小写转换操作方法
方法一:
使用C语言之前的方法,使用函数,进行转换
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "ABCDEFG";
for( int i = 0; i < s.size(); i++ )
{
s[i] = tolower(s[i]);
}
cout<<s<<endl;
return 0;
}
方法二:
通过STL的transform算法配合的toupper和tolower来实现该功能
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string s = "ABCDEFG";
string result;
transform(s.begin(),s.end(),s.begin(),::tolower);
cout<<s<<endl;
return 0;
}
补充:C++ string大小写转换
1、通过单个字符转换,使用C的toupper、tolower函数实现
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string str = "ancdANDG";
cout << "转换前的字符串: " << str << endl;
for(auto &i : str){
i = toupper(i);//i = tolower(i);
}
cout << "转换后的字符串: " << str << endl;
//或者
for(int i = 0;i < str.size();++i){
str[i] = toupper(s[i]);//str[i] = toupper(s[i]);
}
cout << "转换后的字符串: " << str << endl;
return 0;
}
2、通过STL的transform实现
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string str = "helloWORLD";
cout << "转换前:" << str << endl;
//全部转换为大写
transform(str.begin(), str.end(), str.begin(), ::toupper);
cout << "转换为大写:" << str << endl;
//全部转换为小写
transform(str.begin(), str.end(), str.begin(), ::tolower);
cout << "转换为小写:" << str << endl;
//前五个字符转换为大写
transform(str.begin(), str.begin()+5, str.begin(), ::toupper);
cout << "前五个字符转换为大写:" << str << endl;
//后五个字符转换为大写
transform(str.begin()+5, str.end(), str.begin()+5, ::toupper);
cout << "前五个字符转换为大写:" << str << endl;
return 0;
}
来源:https://blog.csdn.net/qq_37941471/article/details/81988702
标签:C++,string,大小写转换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot动态调用实现类方式
2021-07-05 21:33:17
Android文件下载功能实现代码
2023-08-14 00:58:55
详解C#中的依赖注入和IoC容器
2023-03-11 09:05:19
![](https://img.aspxhome.com/file/2023/2/82232_0s.png)
Mybatis示例讲解注解开发中的单表操作
2023-08-20 06:20:58
![](https://img.aspxhome.com/file/2023/2/67992_0s.png)
安卓逆向分析之酷狗signature案例分享
2022-12-06 21:23:37
![](https://img.aspxhome.com/file/2023/1/130431_0s.png)
协定需要会话,但是绑定“BasicHttpBinding”不支持它或者因配置不正确而无法支持它
2023-03-17 16:44:34
java集合继承关系图分享
2023-04-25 17:17:23
![](https://img.aspxhome.com/file/2023/0/67480_0s.png)
Java线程池用法实战案例分析
2022-01-22 07:57:16
eclipse 中的javac命令与java命令
2023-08-19 14:16:57
![](https://img.aspxhome.com/file/2023/2/67622_0s.png)
Android 区别真机和模拟器的几种方法
2022-05-19 14:13:44
C#中静态的深入理解
2023-10-18 01:28:27
使用@Value值注入及配置文件组件扫描
2023-12-01 21:24:12
c#使用S22.Imap收剑灵激活码邮件代码示例(imap收邮件)
2022-11-27 20:59:37
springboot集成mybatisPlus+多数据源的实现示例
2023-11-24 22:35:02
![](https://img.aspxhome.com/file/2023/4/59924_0s.png)
C#执行存储过程并将结果填充到GridView的方法
2022-08-08 06:25:38
WCF如何使用动态代理精简代码架构
2023-09-17 16:25:42
使用BufferedReader读取本地文件的操作
2021-10-15 03:22:47
![](https://img.aspxhome.com/file/2023/2/103982_0s.png)
springboot post接口接受json时,转换为对象时,属性都为null的解决
2023-06-17 15:24:23
![](https://img.aspxhome.com/file/2023/3/57313_0s.png)
java实现163邮箱发送邮件到qq邮箱成功案例
2023-09-18 02:38:09
Android中Canvas的常用方法总结
2021-11-25 03:00:28
![](https://img.aspxhome.com/file/2023/1/111721_0s.png)