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;
}