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函数

tolower 和 toupper 是 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> 头文件来访问 tolower 和 toupper 函数。这些函数对于在字符串处理、字符比较或大小写敏感的操作中很有用。

三、ASCLL码

附表如下:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值