字符串字母变大写

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;
}

おすすめ

転載: blog.csdn.net/xiaoshazheng/article/details/106796199