In C++, there is a package (from C) that handles single characters.
#include <cctype> (ctype.h)
There are some common functions, such as judging whether uppercase letters, lowercase letters, and numbers are registered, etc., as well as conversion, turning uppercase letters into lowercase letters, and lowercase letters into uppercase letters.
Record some common functions use, first of all we need to know, all of these functions return int
type, so remember to use when needed.
#include<cctype>
#include<string>
using namespace std;
int main()
{
string s = "Hello";
for (auto& ch : s) {
if (isupper(ch)) {
ch = tolower(ch);
}
}
cout << s << endl;
//输出 hello
return 0;
}
判断是否是大写字母 int isupper(int c);
判断是否是小写字母 int islower(int c);
判断是否是字母 int isalpha(int c);
判断是否是十进制数字 int isdigit(int c);
判断是否是数字字母 int isalnum(int c);
转变函数:
小写字母转变为大写字母 int toupper(int c);
大写字母转变魏小写字母 int tolower(int c);
实现原理:
小写转大写 ch - 'a' + 'A';
大写转小写 ch - 'A' + 'a';
The distinguishing character function of cctype.
Here also record the commonly used ASCII codes of alphanumerics.
-
字符 十进制 十六进制
-
0 48 0x30
-
A 65 0x41
-
Z 90 0x5A
-
a 97 0x61
-
z 122 0x7A
A total of 26 lowercase letters