c++ primer习题9.34, 9.35, 9.40

使用迭代器将string对象中的字符都改成大写字母ios

#include <iostream>
#include <string>
#include <cctype>
using namespace std;


int main()
{
    string str="This is a example";
    for(string::iterator iter=str.begin();iter!=str.end(); ++iter)
    {
        *iter=toupper(*iter);//将字符转换为对应的大写字母
    }
    //输出查看结果
    cout<<str<<endl;
    return 0;

}


使用迭代器寻找和删除string对象中全部的大写字符app

#include <iostream>
#include <string>
#include <cctype>
using namespace std;


int main()
{
    string str="This IS A example";
    for(string::iterator iter=str.begin();iter!=str.end(); ++iter)
    {
        if(isupper(*iter))
        {
            str.erase(iter);
            --iter;
        }
    }
    //输出查看结果
    cout<<str<<endl;
    return 0;

}


#include <iostream>
#include <string>
#include <cctype>
using namespace std;


int main()
{
    string q1("When lilacs last in the dooryard bloom'd");
    string q2("The child is father of the man");
    string sentence;

    //将sentence赋值为"The child is "
    sentence.assign(q2.begin(),q2.begin()+13);
    //在sentence末尾添加"in the dooryard"
    sentence.append(q1.substr(q1.find("in"), 15));


    //输出查看结果
    cout<<sentence<<endl;
    return 0;

}
相关文章
相关标签/搜索