【C++ Primer】【练习题】【3.14】将vector<string>对象各元素中的小写字母转换成大写字母

来源:互联网 发布:sql语句去掉重复数据 编辑:程序博客网 时间:2024/05/29 08:07

要求:读一组单词到vector<string>对象,将对象各元素中的小写字母转换成大写字母并输出。

#include <iostream>#include <vector>#include <string>#include <cctype>using namespace std;int main(){    vector<string> svec;        string str;    cout << "Please input text(EOF to end): ";    while (cin >> str)    {        svec.push_back(str);    }    if (svec.size() == 0)    {        cout << "No text input!" << endl;        return 0;    }        cout << "The output words are: " << endl;    // 将元素中的小写字母转换成大写字母    for (vector<string>::size_type i = 0; i < svec.size(); i++)    {                for (string::size_type j = 0; j < svec[i].size(); j++)                {                        if (islower(svec[i][j]))                        {                                svec[i][j] = toupper(svec[i][j]);                        }                }                cout << svec[i] << " ";                if ((i + 1) % 8 == 0)                {                        cout << endl;                }    }            cout << endl;    return 0;}  

[chapter3]$ ./a.out Please input text(EOF to end): No text input! [chapter3]$ ./a.out Please input text(EOF to end): a B c D e F g The output words are: A B C D E F G [chapter3]$ ./a.out Please input text(EOF to end): a B c D e F g H i J k L m N o P q R s T u V w X y Z The output words are: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z


0 0
原创粉丝点击