[C_ST49-易] 字串取代
問題描述:
目前有一篇文章,在文章中有些字串內容不雅或不適當,而現在為了美觀我們要改掉這些不雅或不適當的字眼,請依據字串修改對照表來取代。
輸入說明:
輸入一文章,將字串修改對照表中列為不雅字的單字取代掉。
※文章字元長度(包含空白)<=1000。
範例:
輸出輸入測試資料
目前有一篇文章,在文章中有些字串內容不雅或不適當,而現在為了美觀我們要改掉這些不雅或不適當的字眼,請依據字串修改對照表來取代。
字串修改對照表 | |
不雅字 | 取代字 |
hate | love |
stupid | smart |
angry | happy |
dirty | clean |
輸入一文章,將字串修改對照表中列為不雅字的單字取代掉。
※文章字元長度(包含空白)<=1000。
範例:
輸出輸入測試資料
Sample Input: | Sample Output: |
To tell you the truth, I hate to do it,The sailor was stupid with liquor,They were drinking and telling dirty jokes.He'll be angry to find that nothing has been done. | To tell you the truth, I love to do it,The sailor was smart with liquor,They were drinking and telling clean jokes.He'll be happy to find that nothing has been done. |
To tell you the truth, I hate to do it,The sailor was stupid with liquor,They were drinking and telling dirty jokes.He'll be angry to find that nothing has been done. To tell you the truth, I hate to do it,The sailor was stupid with liquor,They were drinking and telling dirty jokes.He'll be angry to find that nothing has been done. | To tell you the truth, I love to do it,The sailor was smart with liquor,They were drinking and telling clean jokes.He'll be happy to find that nothing has been done.To tell you the truth, I love to do it,The sailor was smart with liquor,They were drinking and telling clean jokes.He'll be happy to find that nothing has been done. |
- #include<iostream>
- #include<string>
- #include<algorithm>
- #include<map>
- using namespace std;
- int main()
- {
- // [C_ST49-易] 字串取代
- string s;
- getline(cin, s);
- string p[] = { "hate", "stupid", "angry", "dirty" };
- string q[] = { "love", "smart", "happy", "clean" };
- for (int i = 0; i < 4; i++)
- {
- string p1 = p[i];
- string q1 = q[i];
- int fpos = 0;
- while (1)
- {
- fpos = s.find(p1, fpos);
- if (fpos != string::npos)
- {
- s.replace(fpos, p1.size(), q1);
- fpos = fpos + p1.size();
- }
- else
- break;
- }
- }
- cout << s << endl;
- return 0;
- }
沒有留言:
張貼留言