3.1蓝桥杯每日一个知识点,大小写转化

islower/isupper函数

  • C++中的字符分类函数,用于检查一个字符的大小写
  • 需要包含头文件<cctype>或者万能头文件<bit/stdc++.h>
  • 函数返回值1为bool类型
if(islower(ch1)){
cout << ch1 << "is a lowercase letter." << endl;
} else{
cout << ch1 << "is not a lowercase letter." << endl;
}//isupper   类似

tolower/toupper函数

tolower(char ch)可以将ch转化为小写字母,如果ch不是大写字母则不能进行操作
toupper()同理

char lowercaseCh1 = tolower(ch1);
char uppercaseCh2 = toupper(ch2);

ascii码

0-9 对应ASCII码 48-57
A-Z 对应ASCII码 65-90
a-z 对应ASCII码 97-122
image
!!程序设计时要注意到底时字符还是数字

例题

#include<bits/stdc++.h>
using namespace std;
char convertedCh(char ch)
{
    if('a' <= ch && ch <= 'z')ch = ch - 'a' + 'A';
    else if('A' <= ch && ch < 'Z')ch = ch - 'A' + 'a';
    return ch;
}
int main()
{
    string s;getline(cin,s);//getline函数用于从标准输入流中读取一行字符串,存储到指定的字符串变量s中。

    for(auto &i : s)
        i = convertedCh(i);
    cout << s << '\n';
    return 0;
}

错误麻烦评论区指出

posted @   777CC  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示