2017年11月26日 星期日

[C_ST49-易] 字串取代(C++)

[C_ST49-易] 字串取代

問題描述:
目前有一篇文章,在文章中有些字串內容不雅或不適當,而現在為了美觀我們要改掉這些不雅或不適當的字眼,請依據字串修改對照表來取代。

字串修改對照表
不雅字取代字
hatelove
stupidsmart
angryhappy
dirtyclean
 
輸入說明:
輸入一文章,將字串修改對照表中列為不雅字的單字取代掉。
※文章字元長度(包含空白)<=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.


  1. #include<iostream>  
  2. #include<string>  
  3. #include<algorithm>  
  4. #include<map>  
  5. using namespace std;  
  6. int main()  
  7. {  
  8.     // [C_ST49-易] 字串取代  
  9.     string s;  
  10.     getline(cin, s);  
  11.     string p[] = { "hate""stupid""angry""dirty" };  
  12.     string q[] = { "love""smart""happy""clean" };  
  13.     for (int i = 0; i < 4; i++)  
  14.     {  
  15.         string p1 = p[i];  
  16.         string q1 = q[i];  
  17.         int fpos = 0;  
  18.         while (1)  
  19.         {  
  20.             fpos = s.find(p1, fpos);  
  21.             if (fpos != string::npos)  
  22.             {  
  23.                 s.replace(fpos, p1.size(), q1);  
  24.                 fpos = fpos + p1.size();  
  25.             }  
  26.             else  
  27.                 break;  
  28.         }  
  29.     }  
  30.     cout << s << endl;  
  31.     return  0;  
  32. }  

沒有留言:

張貼留言