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;
}
  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
//--------------------Add_info.h-----------------------// #ifndef Add_info_h #define Add_info_h #include"Goodnode.h" //------------------------添加节点(货物信息)的函数-------------------------// int Add_Goodnode(int Goodnum,char *Goodname,char *exp,char *pubname,int innum,float buyp,float sellp,Good* last); //-----------------------添加货物信息模块-----------------------// void Add_info(Good *p); #endif //--------------------Attached_fun.h----------------// #ifndef Attached_fun_h #define Attached_fun_h //-----------------------------------计算盈利函数----------------------------------// template //函数模板 T Cal_income(Good *p,T m) //??? { T total_income; //定义总盈利变量total_income total_income=((p->Sellp)-(p->Buyp))*(p->Innum); return total_income; } //-----------------------------------显示盈利函数----------------------------------// void Show_income(Good *p); //---------------------------显示盈利最多的货物的信息函数---------------------------// void Show_max_income(Good *p); //----------------------------显示盈利最少的货物的信息函数----------------------------// void Show_min_income(Good *p); //--------------------------------显示全部盈利函数------------------------------------// void Show_all_income(Good *p); //----------------------------------附加功能模块--------------------------------------// void Attached_fun(Good *p); #endif //-------------------Delete_info.h------------------// #ifndef Delete_info_h #define Delete_info_h #include"Goodnode.h" //--------------------显示所有货物信息的函数-----------------// void Show_info(Good *p); //--------------------删除节点函数-----------------------// void Delete_node(Good *pr,Good *p); //------------------------删除货物信息模块-----------------// void Delete_info(Good *p); #endif //--------------------Goodclass.h--------------------// //---------------------------------------------货物类头文件-----------------------------------------------// #ifndef Goodclass_h //条件编译 #define Goodclass_h #include using namespace std; class Data //定义数据类 { protected:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值