日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++?如何將string轉換成全小寫_C 語言

作者:zing2000 ? 更新時間: 2022-12-09 編程語言

如何將string轉換成全小寫

#include <iostream>
#include <string>
#include <algorithm>
 
using std::cout;
using std::endl;
 
void main()
{
        std::string str;
	str.assign("Hello World!");
 
	std::transform(str.begin(),str.end(),str.begin(),tolower); // or 'toupper'.
 
	cout<<str.c_str()<<endl;
}

string字符串大小寫轉換的兩種方式

這里提供兩種對c++中string字符串進行大小寫轉換的方式(windows系統vs)

第一種方式:下標

#include<iostream>
#include<string>

using namespace std;

int main()
{
?? ?string str;
?? ?cin >> str; ? ?//注意這里對于中間有空格的單詞只會將第一個空格前的單詞大寫
?? ?//getline(cin, str); ? ? 可以將一整行的單詞大寫,兩種方式看個人需求取其一即可
?? ?for (int i = 0; i < str.size(); i++)
?? ??? ?str[i] = toupper(str[i]);
?? ?cout << str << endl;
?? ?return 0;
}

第二種方式:迭代器

#include<iostream>
#include<string>

using namespace std;

int main()
{
?? ?string str;
?? ?//cin >> str; ? ?//注意這里對于中間有空格的單詞只會將第一個空格前的單詞大寫
?? ?getline(cin, str); ? ? //可以將一整行的單詞大寫,兩種方式看個人需求取其一即可
?? ?for (auto it1 = str.begin(); it1 != str.end(); it1++)
?? ?{
?? ??? ?*it1 = toupper(*it1);
?? ?}
?? ?cout << str << endl;
?? ?return 0;
}

//另外如果要將單詞化為小寫,將toupper換成tolower即可

原文鏈接:https://blog.csdn.net/zing2008/article/details/103557204

欄目分類
最近更新