C++常用库函数大小写转换

在我们在编写代码时大小写转换是基础知识,这篇博客将通过介绍C++常用库函数来回顾和学习一种不一样的大小写转换

一、islower/isupper函数

islowerisupper函数是C++标准库中的字符分类函数,用于检查一个字符是否为小写字母或大写字母。
islowerisupper函数的使用需要包含头文件<cctype>, 也可用万能头<bits/stdc++.h>包含。
用法如下:

#include<iostream>
#include<algorithm>
#include<cctype>//需要引入这个头文件

using namespace std;

int main()
{
    char ch1 = 'A';
    char ch2 = 'b';
    
    cout << islower(ch1) << endl; //A不是小写,返回0
    cout << isupper(ch1) << endl; //A是小写,返回非0
    
    cout << islower(ch2) << endl; //b是小写,返回非0
    cout << isupper(ch2) << endl; //b不是大写,返回0
    
    return 0;
}

二、tolower/toupper函数

tolowertoupper 是 C++ 标准库中的函数,用于转换字符的大小写。

tolower 函数接受一个字符作为参数,并返回其对应的小写字母字符。如果参数不是大写字母,则返回原始字符。函数原型如下:

int tolower(int c);

下面是一个示例,演示如何使用 tolower 函数将字符转换为小写:

#include <iostream>
#include <cctype>

int main() {
    char ch = 'A';
    char lowercaseCh = tolower(ch);
    std::cout << lowercaseCh << std::endl;  // 输出 'a'

    return 0;
}

toupper 函数类似地接受一个字符作为参数,并返回其对应的大写字母字符。如果参数不是小写字母,则返回原始字符。函数原型如下:

int toupper(int c);

下面是一个示例,演示如何使用 toupper 函数将字符转换为大写:

#include <iostream>
#include <cctype>

int main() {
    char ch = 'a';
    char uppercaseCh = toupper(ch);
    std::cout << uppercaseCh << std::endl;  // 输出 'A'

    return 0;
}

在这两个示例中,我们包含了 <cctype> 头文件来访问 tolowertoupper 函数。这些函数对于在字符串处理、字符比较或大小写敏感的操作中很有用。

三、ASCLL码

附表如下:
在这里插入图片描述
在了解ascll码后,我们可以通过对英文字符的加减运算进行字符串的转换,特别注意三值:
a 97
A 65
0 48
大小写字符差值为32,字符数字与数字之间差个‘0’ ,也就是48.

这就是这一篇的全部全部内容了,喜欢可以点点赞,假期会持续更新,感谢您的支持。

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pigwantofly

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值