Board logo

標題: [6/8 作業4] replace() 函式 [打印本頁]

作者: tonyh    時間: 2024-6-8 11:40     標題: [6/8 作業4] replace() 函式

試以 replace() 函式,將字串中的目標對象以特定字串(或字元)取代。
<string> 標頭檔所提供的 replace() 函式可將字串中特定位置及長度的字串,以另一個字串取代;而 <algorithm> 標頭檔所提供的 replace() 函式可將字串中所有的特定字元,以另一個字元取代。
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str1="honolulu";
  9.     cout<<str1.replace(1,3,"a")<<endl;  //halulu

  10.     string str2="honolulu";
  11.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;  //honolalu

  12.     string str3="honolulu";
  13.     replace(str3.begin(),str3.end(),'u','a');
  14.     cout<<str3<<endl;     //honolala

  15.     system("pause");
  16.     return 0;
  17. }
複製代碼

作者: 邱奕倫    時間: 2024-6-9 20:54

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str1="honolulu";
  9.     cout<<str1.replace(1,3,"a")<<endl;  //halulu

  10.     string str2="honolulu";
  11.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;  //honolalu

  12.     string str3="honolulu";
  13.     replace(str3.begin(),str3.end(),'u','a');
  14.     cout<<str3<<endl;     //honolala

  15.     system("pause");
  16.     return 0;
  17. }
複製代碼

作者: 張仲言    時間: 2024-6-10 10:32

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str1="honolulu";
  9.     cout<<str1.replace(1,3,"a")<<endl;  //halulu

  10.     string str2="honolulu";
  11.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;  //honolalu

  12.     string str3="honolulu";
  13.     replace(str3.begin(),str3.end(),'u','a');
  14.     cout<<str3<<endl;     //honolala

  15.     system("pause");
  16.     return 0;
  17. }
複製代碼

作者: 陳煒翰    時間: 2024-6-10 16:28

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str1="honolulu";
  9.     cout<<str1.replace(1,3,"a")<<endl;  //halulu

  10.     string str2="honolulu";
  11.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;  //honolalu

  12.     string str3="honolulu";
  13.     replace(str3.begin(),str3.end(),'u','a');
  14.     cout<<str3<<endl;     //honolala

  15.     system("pause");
  16.     return 0;
  17. }
複製代碼

作者: 廖和風    時間: 2024-6-11 19:46

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str1="honolulu";
  9.     cout<<str1.replace(1,3,"a")<<endl;

  10.     string str2="honolulu";
  11.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;

  12.     string str3="honolulu";
  13.     replace(str3.begin(),str3.end(),'u','a');
  14.     cout<<str3<<endl;

  15.     system("pause");
  16.     return 0;
  17. }
複製代碼

作者: 黃彥綺    時間: 2024-6-12 21:40

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str1="honolulu";
  9.     cout<<str1.replace(1,3,"a")<<endl;
  10.     string str2="honolulu";
  11.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;
  12.     string str3="honolulu";
  13.     replace(str3.begin(),str3.end(),'u','a');
  14.     cout<<str3<<endl;
  15.     system("pause");
  16.     return 0;
  17. }
複製代碼

作者: 林宜靜    時間: 2024-6-13 22:26

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     string str1="honolulu";
  6.     cout<<str1.replace(1,3,"a")<<endl;
  7.     string str2="honolulu";
  8.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;
  9.     string str3="honolulu";
  10.     replace(str3.begin(),str3.end(),'u','a');
  11.     cout<<str3<<endl;
  12.     system("pause");
  13.     return 0;
  14. }
複製代碼

作者: 曾湘鋆    時間: 2024-6-14 22:18

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str1="honolulu";
  9.     cout<<str1.replace(1,3,"a")<<endl;
  10.     string str2="honolulu";
  11.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;
  12.     string str3="honolulu";
  13.     replace(str3.begin(),str3.end(),'u','a');
  14.     cout<<str3<<endl;
  15.     system("pause");
  16.     return 0;
  17. }
複製代碼

作者: 陳祈安    時間: 2024-6-14 23:59

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str1="honolulu";
  9.     cout<<str1.replace(1,3,"a")<<endl;

  10.     string str2="honolulu";
  11.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;

  12.     string str3="honolulu";
  13.     replace(str3.begin(),str3.end(),'u','a');
  14.     cout<<str3<<endl;

  15.     system("pause");
  16.     return 0;
  17. }
複製代碼

作者: 邱思博    時間: 2024-6-15 07:43

  1. #include<bits/strc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     string str1="honolulu";
  6.     cout<<str1.replace(1,3,"a")<<endl;  

  7.     string str2="honolulu";
  8.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;

  9.     string str3="honolulu";
  10.     replace(str3.begin(),str3.end(),'u','a');
  11.     cout<<str3<<endl;   
  12.     system("pause");
  13.     return 0;
  14. }
複製代碼

作者: 吳睿晏    時間: 2024-6-15 08:59

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     string str1="honolulu";
  6.     cout<<str1.replace(1,3,"a")<<endl;
  7.     string str2="honolulu";
  8.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;
  9.     string str3="honolulu";
  10.     replace(str3.begin(),str3.end(),'u','a');
  11.     cout<<str3<<endl;
  12.     system("pause");
  13.     return 0;
  14. }
複製代碼

作者: 鄧維成    時間: 2024-6-15 09:58

本帖最後由 鄧維成 於 2024-6-15 10:25 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str1="honolulu";
  9.     cout<<str1.replace(1,3,"a")<<endl; //halulu

  10.     string str2="honolulu";
  11.     cout<<str2.replace(str2.find("u"),1,"a")<<endl; //honolalu

  12.     string str3="honolulu";
  13.     replace(str3.begin(),str3.end(),'u','a');
  14.     cout<<str3<<endl; //honolalaaa

  15.     system("pause");
  16.     return 0;
  17. }
複製代碼

作者: 林弈呈    時間: 2024-6-15 10:03

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. #include<algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     string str1="honolulu";
  9.     cout<<str1.replace(1,3,"a")<<endl;  //halulu

  10.     string str2="honolulu";
  11.     cout<<str2.replace(str2.find("u"),1,"a")<<endl;  //honolalu

  12.     string str3="honolulu";
  13.     replace(str3.begin(),str3.end(),'u','a');
  14.     cout<<str3<<endl;     //honolala

  15.     system("pause");
  16.     return 0;
  17. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2