C++ 程序将字符串转换为大写

编写一个 C++ 程序,将字符串转换为大写,并附带示例。在此 C++ 程序中,我们使用 for 循环(for (int i = 0; i < lwTxt.length(); i++)) 来遍历 lwTxt 字符串的字符。在其中,我们使用 toupper 函数(lwTxt[i] = toupper(lwTxt[i])) 将小写字符串转换为大写。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string lwTxt;
	
	cout << "\nPlease Enter the String to Convert into Uppercase  =  ";
	getline(cin, lwTxt);
	
	for (int i = 0; i < lwTxt.length(); i++)
  	{
  		lwTxt[i] = toupper(lwTxt[i]);
  	}
  	
	cout<< "\nThe Given String in Uppercase = " << lwTxt;
		
 	return 0;
}
Program to Convert String to Uppercase

 算法文件有一个 transform 函数,可以将小写字符串转换为大写。

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
	string lwTxt;
	
	cout << "\nPlease Enter the String to Convert into Uppercase  =  ";
	getline(cin, lwTxt);
	
	std:: transform(lwTxt.begin(), lwTxt.end(), lwTxt.begin(), ::toupper);
	
	cout<< "\nThe Given String in Uppercase = " << lwTxt;
		
 	return 0;
}
Please Enter the String to Convert into Uppercase  =  hello world

The Given String in Uppercase = HELLO WORLD

使用 While 循环将字符串转换为大写的 C++ 示例

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string lwTxt;
	int i = 0; 
	
	cout << "\nPlease Enter the String to Convert into Uppercase  =  ";
	getline(cin, lwTxt);
	
	while(i < lwTxt.length())
  	{
  		if(islower(lwTxt[i]))
  		{
  			lwTxt[i] = toupper(lwTxt[i]);
		}
		i++;
  	}
  	
	cout<< "\nThe Given String in Uppercase = " << lwTxt;
		
 	return 0;
}
Please Enter the String to Convert into Uppercase  =  c++ Programs

The Given String in Uppercase = C++ PROGRAMS

在此将字符串转换为大写的程序 代码 中,我们没有转换所有字符,而是使用 if 语句(if(lwTxt[i] >= ‘a’ && lwTxt[i] <= ‘z’)) 来检查字符是否为小写。如果为真,我们将 ASCII 值减去 32 以获得大写字符。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string lwTxt;
	
	cout << "\nPlease Enter the String to Convert into Uppercase  =  ";
	getline(cin, lwTxt);
	
	for (int i = 0; i < lwTxt.length(); i++)
  	{
  		if(lwTxt[i] >= 'a' && lwTxt[i] <= 'z')
  		{
  			lwTxt[i] = lwTxt[i] - 32;
		}
  	}
  	
	cout<< "\nThe Given String in Uppercase = " << lwTxt;
		
 	return 0;
}
Please Enter the String to Convert into Uppercase  =  tutorial gateway

The Given String in Uppercase = TUTORIAL GATEWAY

在此将字符串转换为大写的示例中,我们使用了 If 语句。在 If 语句(if(lwTxt[i] >= 97 && lwTxt[i] <= 122 )) 中,我们使用 ASCII 值来标识字符串中的小写字符。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string lwTxt;
	
	cout << "\nPlease Enter the String to Convert into Uppercase  =  ";
	getline(cin, lwTxt);
	
	for (int i = 0; i < lwTxt.length(); i++)
  	{
  		if(lwTxt[i] >= 97 && lwTxt[i] <= 122 )
  		{
  			lwTxt[i] = lwTxt[i] - 32;
		}
  	}
  	
	cout<< "\nThe Given String in Uppercase = " << lwTxt;
		
 	return 0;
}
Please Enter the String to Convert into Uppercase  =  tutorialgateway

The Given String in Uppercase = TUTORIALGATEWAY

使用函数的 C++ 程序将字符串转换为大写

#include<iostream>
#include<string>
using namespace std;

string stringUpper(string lwTxt)
{
	for (int i = 0; i < lwTxt.length(); i++)
  	{
  		if(lwTxt[i] >= 'a' && lwTxt[i] <= 'z')
  		{
  			lwTxt[i] = lwTxt[i] - 32;
		}
  	}
  	return lwTxt;
}
int main()
{
	string lwTxt;
	
	cout << "\nPlease Enter the String to Convert into Uppercase  =  ";
	getline(cin, lwTxt);
	
	string up = stringUpper(lwTxt);
  	
	cout<< "\nThe Given String in Uppercase = " << up;
		
 	return 0;
}
Please Enter the String to Convert into Uppercase  =  hello 2020 world!

The Given String in Uppercase = HELLO 2020 WORLD!