C++中字符串大小写字母转换

最近在学习 STL,string 也是 STL 中的一种容器,遇到一个字符串中字母大小写转换的例子,这里就顺便总结一下在C++中常用的字符串大小写转换方法,有需要的可以参考。代码如下:

1、char[]类型,调用库函数
//==========================================										   	
//	Filename : 1、char[]类型,调用库函数						   	
//	Time     : 2019年5月6日					   
//	Authonr  : 柚子树					   	
//	Email    : [email protected]		   										   
//==========================================

#include 
#include 
#include 

using namespace std;

void myToupper(char* str)
{
	int length = strlen(str);
	for (size_t i = 0; i < length; i++)
	{
		if (str[i] >= 'a' && str[i] <= 'z')
		{
			str[i] = toupper(str[i]);
		}
	}
}

void myTolower(char* str)
{
	int length = strlen(str);
	for (size_t i = 0; i < length; i++)
	{
		if (str[i] >= 'A' && str[i] <= 'Z')
		{
			str[i] = tolower(str[i]);
		}
	}
}

int main()
{
	char str[20] = { 0 };
	cout << "请输入一个包含大小写字母的字符串: " << endl;
	cin.getline(str, 20);

	myTolower(str);
	cout << "转小写: " << str << endl;

	myToupper(str);
	cout << "转大写: " << str << endl;
	system("pause");
	return EXIT_SUCCESS;
}

运行结果:
C++中字符串大小写字母转换_第1张图片

2、char[]类型,自定义转换函数
//==========================================										   	
//	Filename : 2、char[]类型,自定义转换函数					   	
//	Time     : 2019年5月6日
//	Authonr  : 柚子树					   	
//	Email    : [email protected]		   										   
//==========================================

#include 
#include 
#include 
using namespace std;

void myToupper(char* str)
{
	int length = strlen(str);
	for (size_t i = 0; i < length; i++)
	{
		if (str[i] >= 'a' && str[i] <= 'z')
		{
			str[i] -= 32;
			// str[i] = str[i] - 'a' + 'A';
		}
	}
}

void myTolower(char* str)
{
	int length = strlen(str);
	for (size_t i = 0; i < length; i++)
	{
		if (str[i] >= 'A' && str[i] <= 'Z')
		{
			str[i] += 32;
			// str[i] = str[i] - 'A' + 'a';
		}
	}
}

int main()
{
	char str[20] = { 0 };
	cout << "请输入一个包含大小写字母的字符串: " << endl;
	cin.getline(str, 20);

	myTolower(str);
	cout << "转小写: " << str << endl;

	myToupper(str);
	cout << "转大写: " << str << endl;

	system("pause");
	return EXIT_SUCCESS;
}

运行结果:
C++中字符串大小写字母转换_第2张图片

3、string类型,调用库函数
//==========================================							   	
//	Filename : 3、string类型,调用库函数					   	
//	Time     : 2019年5月6日					   
//	Authonr  : 柚子树					   	
//	Email    : [email protected]		   										   
//==========================================

#include 
#include 
#include 
#include 
using namespace std;

string str;

int main()
{
	cout << "请输入一个包含大小写字母的字符串: " << endl;
	cin >> str;

	transform(str.begin(), str.end(), str.begin(), ::tolower);
	cout << "转小写: " << str << endl;

	transform(str.begin(), str.end(), str.begin(), ::toupper);
	cout << "转大写: " << str << endl;

	system("pause");
	return EXIT_SUCCESS;
}

运行结果:
C++中字符串大小写字母转换_第3张图片

4、string类型,自定义实现转换
//==========================================										   	
//	Filename : 4、string类型,自定义实现转换					   	
//	Time     : 2019年5月6日					   
//	Authonr  : 柚子树					   	
//	Email    : [email protected]		   										   
//==========================================

#include 
#include 
#include 
#include 
using namespace std;

void myToupper(string &str)
{
	int length = str.size();
	for (size_t i = 0; i < length; i++)
	{
		if (str[i] >= 'a' && str[i] <= 'z')
		{
			str[i] -= 32;
			// str[i] = str[i] - 'a' + 'A';
		}
	}
}

void myTolower(string &str)
{
	int length = str.size();
	for (size_t i = 0; i < length; i++)
	{
		if (str[i] >= 'A' && str[i] <= 'Z')
		{
			str[i] += 32;
			// str[i] = str[i] - 'a' + 'A';
		}
	}
}

int main()
{
	string str;
	cout << "请输入一个包含大小写字母的字符串: " << endl;
	cin >> str;

	myTolower(str);
	cout << "转小写: " << str << endl;

	myToupper(str);
	cout << "转大写: " << str << endl;

	system("pause");
	return EXIT_SUCCESS;
}

运行结果:
C++中字符串大小写字母转换_第4张图片

你可能感兴趣的:(C/C++,字符串,C/C++,字母大小写转换)