编写一个 C++ 程序,将字符串转换为小写,并附带示例。在此 C++ 程序中,我们使用 for 循环遍历 upTxt 字符串的字符。我们在循环中使用 tolower 函数将大写字符串转换为小写。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string upTxt;
cout << "\nPlease Enter the String to Convert into Lowercase = ";
getline(cin, upTxt);
for (int i = 0; i < upTxt.length(); i++)
{
upTxt[i] = tolower(upTxt[i]);
}
cout<< "\nThe Given String in Lowercase = " << upTxt;
return 0;
}

该程序使用算法文件下的 std transform 函数将大写字符串转换为小写。
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string upTxt;
cout << "\nPlease Enter the String to Convert into Lowercase = ";
getline(cin, upTxt);
std:: transform(upTxt.begin(), upTxt.end(), upTxt.begin(), ::tolower);
cout<< "\nThe Given String in Lowercase = " << upTxt;
return 0;
}
Please Enter the String to Convert into Lowercase = HELLo WolLD!
The Given String in Lowercase = hello wolld!
C++ 使用 While 循环将字符串转换为小写的示例
#include<iostream>
#include<string>
using namespace std;
int main()
{
string upTxt;
int i = 0;
cout << "\nPlease Enter the String to Convert into Lowercase = ";
getline(cin, upTxt);
while(i < upTxt.length())
{
if(isupper(upTxt[i]))
{
upTxt[i] = tolower(upTxt[i]);
}
i++;
}
cout<< "\nThe Given String in Lowercase = " << upTxt;
return 0;
}
Please Enter the String to Convert into Lowercase = TutORIAL GATEWAY
The Given String in Lowercase = tutorial gateway
在此程序 代码 中,用于将字符串转换为小写,我们使用 if 语句检查字符是否为大写。如果为真,我们将 ASCII 值加 32 来获得小写字符。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string upTxt;
cout << "\nPlease Enter the String to Convert into Lowercase = ";
getline(cin, upTxt);
for (int i = 0; i < upTxt.length(); i++)
{
if(upTxt[i] >= 'A' && upTxt[i] <= 'Z')
{
upTxt[i] = upTxt[i] + 32;
}
}
cout<< "\nThe Given String in Lowercase = " << upTxt;
return 0;
}
Please Enter the String to Convert into Lowercase = c++ PROGRAMS
The Given String in Lowercase = c++ programs
在此字符串转换为小写的示例中,我们使用了 If 语句。在 If 语句中,我们使用 ASCII 值来识别字符串中的大写字符。然后,我们将它们转换为小写。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string upTxt;
cout << "\nPlease Enter the String to Convert into Lowercase = ";
getline(cin, upTxt);
for (int i = 0; i < upTxt.length(); i++)
{
if(upTxt[i] >= 65 && upTxt[i] <= 90)
{
upTxt[i] = upTxt[i] + 32;
}
}
cout<< "\nThe Given String in Lowercase = " << upTxt;
return 0;
}
Please Enter the String to Convert into Lowercase = HI GUYS
The Given String in Lowercase = hi guys
C++ 使用函数将字符串转换为小写的程序
#include<iostream>
#include<string>
using namespace std;
string stringLower(string upTxt)
{
for (int i = 0; i < upTxt.length(); i++)
{
if(upTxt[i] >= 'A' && upTxt[i] <= 'Z')
{
upTxt[i] = upTxt[i] + 32;
}
}
return upTxt;
}
int main()
{
string upTxt;
cout << "\nPlease Enter the String to Convert into Lowercase = ";
getline(cin, upTxt);
string lw = stringLower(upTxt);
cout<< "\nThe Given String in Lowercase = " << lw;
return 0;
}
Please Enter the String to Convert into Lowercase = HI 2021 GUys
The Given String in Lowercase = hi 2021 guys