C++字符串大小写转换函数

本文详细介绍如何使用C++标准库中的函数实现字符串从大写到小写及从小写到大写的转换,包括使用char数组和string对象的方法,并介绍了如何利用transform函数简化操作。
部署运行你感兴趣的模型镜像

1.首先我们对char数组进行转换需要用到tolower与uppower(头文件string.h)

#include<iostream>
#include<cstdio>
#include<string.h>
//#include<algorithm>
//#include<string>
//#include<algorithm>
using namespace std;
string s;
char s1[100];
int main(){
    while(~scanf("%s",s1)){
        for(int i =0;i<strlen(s1);++i){
            s1[i] = toupper(s1[i]);//转换为大写
        }
        printf("%s\n",s1);
        for(int i =0;i<strlen(s1);++i){
            s1[i] = tolower(s1[i]);//转换为小写
        }
        printf("%s\n",s1);
    }
}

2.string也可以用上面的函数

#include<iostream>
#include<cstdio>
#include<string.h>
//#include<algorithm>
//#include<string>
//#include<algorithm>
using namespace std;
string s;
char s1[100];
int main(){
    while(cin>>s){
        for(int i =0;i<s.size();++i){
            s[i] = toupper(s[i]);//转换为大写
        }
        cout<<s<<endl;
        for(int i =0;i<s.size();++i){
            s[i] = tolower(s[i]);//转换为小写
        }
        cout<<s<<endl;
    }
}

3.string可以用transform(头文件是algorithm

#include<iostream>
#include<cstdio>
//#include<string.h>
//#include<algorithm>
#include<string>
#include<algorithm>
using namespace std;
string s;
char s1[100];
int main(){
    while(cin>>s){
        transform(s.begin(),s.end(),s.begin(),::toupper);
        cout<<s<<endl;
       transform(s.begin(),s.end(),s.begin(),::tolower);
        cout<<s<<endl;
    }
}

您可能感兴趣的与本文相关的镜像

Anything-LLM

Anything-LLM

AI应用

AnythingLLM是一个全栈应用程序,可以使用商用或开源的LLM/嵌入器/语义向量数据库模型,帮助用户在本地或云端搭建个性化的聊天机器人系统,且无需复杂设置

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值