字母大小写转换

1.islower( ) 和 isupper( ) 函数

  • 函数被包含在头文件 <cctype> 中 ;

  • 这两个函数是用来检测参数(字符)是否为小写字母或者大写字母的,函数返回一个 bool 表示结果;

#include <iostream>
#include <cctype>
using namespace std;

int main() {
    char a = 'a',b = 'B';
    bool retult1 = islower(a),retult2 = isupper(b);
    if(retult1)	cout << "yes ";
    if(retult2) cout << "yes";
    return 0;
}

  • 注意:islower( ) 和 upper( ) 两个函数参数应当为 char 型变量,但输入其他字符和数字均可正常运行且认为其既不是大写字母又不是小写字母,字符串不可以设置为参数;

#include <iostream>
#include <cctype>
//#include <string>
using namespace std;

int main() {
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); 
    char a = 'a',b = 'B';
    int c = 3;
    //string d = "AdvsvscsA";
    bool retult1 = islower(c),retult2 = isupper('\n');
    cout << retult1 << "\n";
    cout << retult2 << "\n";
    return 0;
}

2.tolower( ) 和 toupper( ) 函数

  • tolower( ) 是将参数变为其对应的小写字母,如果其参数不是 char 类型的数据则不进行操作;

  • toupper( ) 同理;

#include <iostream>
#include <cctype>
//#include <string>
using namespace std;

int main() {
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); 
    char a = 'a',b = 'B';
    char u = tolower(a);cout << u << ' ';
	u = toupper(a);cout << u;
    return 0;
}

  • 注意:“不进行操作指的并非不执行这个语句,而是不执行转换大小写的操作,将参数直接置于等号的右边

#include <iostream>
#include <cctype>
//#include <string>
using namespace std;

int main() {
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); 
    char a = '3';
    int c = 51;
    char l;
    char u = tolower(a);cout << (int)u << ' ';
	u = tolower(c);cout << u << ' ';
	l = c;
	cout << l;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值