string的大小写转换

之前也做了一些大小写转换的题,但没好好总结,现在小小说明下~
主要是C++头文件algorithm下的transform调用(algorithm真是个小宝库~)

transform(str.begin(),str.end(),str.begin(),::toupper);
三个参数分别为:字符串起始、字符串结束、字符串返回值存储的位置、大小写

代码如下:

#include <stdio.h>
#include <math.h>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

int main(){
    
    
	string str;
	getline(cin,str);
	transform(str.begin(),str.end(),str.begin(),::toupper); //转大写
	transform(str.begin(),str.end(),str.begin(),::tolower);  //转小写
	cout<<str;
}

猜你喜欢

转载自blog.csdn.net/weixin_42377217/article/details/104965391