返回列表 發帖

[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. }
複製代碼

  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. }
複製代碼

TOP

  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. }
複製代碼

TOP

  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. }
複製代碼

TOP

  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. }
複製代碼

TOP

  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. }
複製代碼

TOP

  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. }
複製代碼

TOP

  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. }
複製代碼

TOP

  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. }
複製代碼

TOP

  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. }
複製代碼

TOP

  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. }
複製代碼

TOP

本帖最後由 鄧維成 於 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. }
複製代碼

TOP

  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. }
複製代碼

TOP

返回列表