#include< cctype>
今天在刷題的過程中,偶然碰到#include< cctype>中的函數,趕緊過來總結一下里面常用的函數。
< cctype>C++ 標準庫頭文件, 此頭文件原作爲 <ctype.h> 存在於 C 標準庫。這是一個擁有許多字符串處理函數聲明的頭文件,這些函數可以用來對單獨字符串進行分類和轉換。
1、isdigit() :
檢查字符是否爲十進制數字,例如:0 1 2 3 4 5 6 7 8 9。
2、islower() :
檢查字符是否爲小寫字母,例如:a b c d
3、isupper():
檢查字符是否爲大寫字母,例如:A B C D
4、tolower() :
將大寫字母轉換爲小寫字母,例如:A>a。
5、toupper():
將小寫字母轉換爲大寫字母,例如:a>A。
(後序會更新)
爲了方便記憶,編寫了個小代碼:
代碼如下:
#include<iostream>
#include<cctype>
using namespace std;
int main()
{
char c;
while(cin>>c){
if(isdigit(c)){
cout<<"數字"<<endl;
}else if(islower(c)){
cout<<"小寫"<<endl;
c=toupper(c);
cout<<"轉換爲大寫"<<c<<endl;
}else if(isupper(c)){
cout<<"大寫"<<endl;
c=tolower(c);
cout<<"轉換爲小寫"<<c<<endl;
}else{
cout<<"不知道"<<endl;
}
}
return 0;
}