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

发布时间:2024-01-08 23:27:48

在我们在编写代码时大小写转换是基础知识,这篇博客将通过介绍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.

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

文章来源:https://blog.csdn.net/WDJ54068923/article/details/135468022
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。