字符串字母變大寫

1 數組遍歷

#include <iostream>

using namespace std;

int main()
{
    string s("Hello World");
    int length=s.length();
    for(int i=0;i<length;i++){
        s[i]=toupper(s[i]);
    }
    cout<<s<<endl;
    return 0;
}

2 使用引用

#include <iostream>
using namespace std;

int main()
{
    string s("Hello World");
    int length = s.length();
    for (char& c : s) {
        c = toupper(c);
    }
    cout << s << endl;
    return 0;
}

迭代器

#include <iostream>
using namespace std;

int main()
{
    string s("Hello World");
    for (auto it=s.begin(); it!=s.end(); it++)
    {
        *it = toupper(*it);
    }
    cout << s << endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章