c++和python3中字符大小写转换函数和字符数字判断函数对比-爱代码爱编程
1 字符大小写转换函数
C++中的字符大小写转换函数toupper()
和tolower()
,头文件为#include <iostream>
。代码示例如下,
#include <iostream>
using namespace std;
int main() {
char a = 'a', b = 'B';
cout << "toupper(a) = " << (char)toupper(a) << endl;
cout << "tolower(b) = " << (char)tolower(b) << endl;
return 0;
}
程序输出为,
toupper(a) = A
tolower(b) = b
python3中的字符大小写转换函数upper()
和lower()
,示例代码如下,
a = 'a'
b = 'B'
print(f'a.upper() = {a.upper()}, b.lower() = {b.lower()}')
程序输出为,
a.upper() = A, b.lower() = b
2 字符数字判断函数
C++中的字符数字判断函数isalpha()
和isdigit()
,头文件为#include <iostream>
。代码示例如下,
#include <iostream>
using namespace std;
int main() {
char a = 'a', b = '1';
//isalpha(a),如果a是一个字母字符,则该函数返回非零值,否则返回 0。
//isdigit(a),如果a是一个数字字符,则该函数返回非零值,否则返回 0。
cout << "isalpha(a) = " << isalpha(a) << ", isdigit(a) = " << isdigit(a) << endl;
cout << "isalpha(b) = " << isalpha(b) << ", isdigit(b) = " << isdigit(b) << endl;
return 0;
}
程序输出为,
isalpha(a) = 1024, isdigit(a) = 0
isalpha(b) = 0, isdigit(b) = 1
python3中字符数字判断函数isalpha()
和isdigit()
,示例代码如下,
a = 'a'
b = '1'
print(f'a.isalpha() = {a.isalpha()}, a.isdigit() = {a.isdigit()}')
print(f'b.isalpha() = {b.isalpha()}, b.isdigit() = {b.isdigit()}')
程序输出为,
a.isalpha() = True, a.isdigit() = False
b.isalpha() = False, b.isdigit() = True