C/C++ 多种方式进行大小写字母转换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/w442863748/article/details/50441288
#include <iostream>
#include <string>
#include <ctype.h>
#include <algorithm>

using namespace std;

void myUpper(char* s)//转大写
{   
	while(*s != '\0')
	{
		if(*s >= 'a' && *s <= 'z')
			*s -= ('a' - 'A');//*s -= 32;
		//*s = toupper(*s);
		s++;
	}
}

void myLower(char* s)//转小写
{   
	while(*s != '\0')
	{
		//if(*s >= 'A' && *s <= 'Z')
		//	*s += ('a' - 'A');//*s += 32;
		*s = tolower(*s);
		s++;
	}
}

void main()
{
	char s[] = "aBcDefgHijKLmn 295 &^%";
	cout << s << endl;
	myUpper(s);//转大写
	cout << s <<  endl;
	myLower(s);//转小写
	cout << s <<  endl;
	cout << "---------------------" << endl;
	string str(s);
	transform(str.begin(), str.end(), str.begin(), toupper);//转大写
	cout << str <<  endl;
	transform(str.begin(), str.end(), str.begin(), tolower);//转小写
	cout << str <<  endl;

	system("pause");
}

运行结果:


猜你喜欢

转载自blog.csdn.net/w442863748/article/details/50441288