কম্পিউটার

সাবস্ট্রিংকে অন্য সাবস্ট্রিং C++ দিয়ে প্রতিস্থাপন করুন


এখানে আমরা দেখব কিভাবে সাবস্ট্রিংকে অন্য সাবস্ট্রিং দিয়ে প্রতিস্থাপন করা যায়। এটি স্ট্রিংয়ের অংশটিকে প্রতিস্থাপন করে যা অক্ষর pos থেকে শুরু হয় এবং লেন অক্ষরগুলিকে বিস্তৃত করে৷

রিপ্লেস ফাংশনের গঠন নিচের মত:

string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);

প্যারামিটারগুলি হল pos :এটি একটি সন্নিবেশ বিন্দু, str :এটি একটি স্ট্রিং অবজেক্ট, লেন :এতে মুছে ফেলার জন্য অক্ষরের সংখ্যা সম্পর্কে তথ্য রয়েছে৷

অ্যালগরিদম

Step 1: Get the main string, and the string which will be replaced. And the match string
Step 2: While the match string is present in the main string:
Step 2.1: Replace it with the given string.
Step 3: Return the modified string

উদাহরণ কোড

#include <iostream>
#include <string>
using namespace std;
int main () {
   string base = "this is a test string.";
   string str2 = "n example";
   string str3 = "sample phrase";
   string str4 = "useful.";
   string str = base;
   str.replace(9,5,str2);
   str.replace(19,6,str3,7,6);
   str.replace(8,10,"just a");
   str.replace(8,6,"a shorty",7);
   str.replace(22,1,3,'!');
   str.replace(str.begin(),str.end()-3,str3);
   str.replace(str.begin(),str.begin()+6,"replace");
   str.replace(str.begin()+8,str.begin()+14,"is coolness",7);
   str.replace(str.begin()+12,str.end()-4,4,'o');
   str.replace(str.begin()+11,str.end(),str4.begin(),str4.end());
   cout << str << '\n';
   return 0;
}

আউটপুট

replace is useful.

  1. C++ এ একটি স্ট্রিং এর অংশ অন্য স্ট্রিং দিয়ে প্রতিস্থাপন করুন

  2. StringBuilder ব্যবহার করে একটি স্ট্রিং প্রতিস্থাপন করুন

  3. C# প্রোগ্রাম একটি স্ট্রিং এর সমস্ত স্পেসকে '%20' দিয়ে প্রতিস্থাপন করতে

  4. পাইথনে অন্য স্ট্রিং দিয়ে একটি স্ট্রিংয়ের সমস্ত ঘটনা কীভাবে প্রতিস্থাপন করবেন?