c++ 判断字符是否是数字或字母(以及大小写转换)

transform 字符串str的大小写转换

transform(str.begin(), str.end(), str.begin(), 转换操作);

#include<bits/stdc++.h>

using namespace std;

int main(){
    
    
	string str = "abcdAA123";
	
	// 将字符串str全部转化为大写 
	transform(str.begin(), str.end(), str.begin(), ::toupper);
	cout << "字符串转化为大写\t\t" << str << endl;
	
	// 将字符串str全部转化为小写 
	transform(str.begin(), str.end(), str.begin(), ::tolower);
	cout << "字符串转化为小写\t\t" << str << endl;
    return 0;
}

在这里插入图片描述

字母的大小写转换

  • toupper 转为大写字母
  • tolower 转为小写字母
#include<bits/stdc++.h>

using namespace std;

int main(){
    
    
	// 将数字对应的字母和字母转化为大写 
	cout << "将数字对应的ASCII转化为大写\t" << (char)toupper(97) << endl;
    cout << "将字母转化为大写\t\t" << (char)toupper('a') << endl;
    
    // 将数字对应的字母和字母转化为小写 
    cout << "将数字对应的ASCII转化为小写\t" << (char)tolower(66) << endl;
    cout << "将字母转化为小写\t\t" << (char)tolower('B') << endl;
    
    return 0;
}

在这里插入图片描述

判断字符是否为字母数字

返回结果为 0代表否
返回结果为 非0代表否

  • isalpha 是否为字母
    • islower 是否为小写字母
    • isupper 是否为大写字母
  • isdigit 是否为数字
  • isalnum 是否为字母或数字
#include<bits/stdc++.h>

using namespace std;

int main(){
    
    
	// 判断是否为字母
	cout << "是否为字母(非0则是)\t\t" << isalpha('e') << endl;
	// 判断是否为大写字母
	cout << "否为大写字母(非0则是)\t\t" << isupper('E') << endl;
	// 判断是否为小写字母
	cout << "是否为小写字母(非0则是)\t" << islower('e') << endl;
	
	// 判断是否为数字 
	cout << "是否为数字(非0则是)\t\t" << isdigit('e') << endl;
	
	// 判断是否为字母或数字
	cout << "是否为字母数字(非0则是)\t" << isalnum('e') << endl;
    return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44635198/article/details/114376051
今日推荐