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;

}
相關文章
相關標籤/搜索