C++對(duì)string進(jìn)行大小寫轉(zhuǎn)換的三種方法文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-787861.html
方法一:下標(biāo)
#include <iostream>
#include <string>
using namespace std;
int main()
{
? ? string s = "ABCDEFG";
? ? for( int i = 0; i < s.size(); i++ )
? ? {
? ?? ???s[i] = tolower(s[i]);
? ? }
? ? cout<<s<<endl;
? ? return 0;
}
方法二:迭代器
#include<iostream>
#include<string>
using namespace std;
int main()
{
? ? string str;
? ? //cin >> str;? ? //注意這里對(duì)于中間有空格的單詞只會(huì)將第一個(gè)空格前的單詞大寫
? ? getline(cin, str);? ???//可以將一整行的單詞大寫,兩種方式看個(gè)人需求取其一即可
? ? for (auto it1 = str.begin(); it1 != str.end(); it1++)
? ? {
? ?? ???*it1 = tolower(*it1);
? ? }
? ? cout << str << endl;
? ? return 0;
}
//另外如果要將單詞化為大寫,將tolower換成toupper即可
方法三:通過(guò)STL的transform算法配合的toupper和tolower來(lái)實(shí)現(xiàn)該功能
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
? ? string s = "ABCDEFG";
? ? string result;
? ? transform(s.begin(),s.end(),s.begin(),::tolower);
? ? cout<<s<<endl;
? ? return 0;
}
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-787861.html
到了這里,關(guān)于C++對(duì)string進(jìn)行大小寫轉(zhuǎn)換的三種方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!