এই টিউটোরিয়ালে, আমরা প্রদত্ত স্ট্রিংকে রূপান্তর করার জন্য একটি প্রোগ্রাম নিয়ে আলোচনা করব যাতে এটি শুধুমাত্র স্বতন্ত্র অক্ষর ধারণ করে।
এই জন্য আমাদের একটি স্ট্রিং প্রদান করা হবে. আমাদের কাজ হল স্ট্রিং এর মধ্য দিয়ে যাওয়া এবং সমস্ত পুনরাবৃত্ত অক্ষর প্রতিস্থাপন করা যেকোন এলোমেলো অক্ষর দিয়ে যা স্ট্রিংটিতে ইতিমধ্যে উপস্থিত নেই৷
উদাহরণ
#include<bits/stdc++.h>
using namespace std;
//collecting the distinct characters
//in the string
int calculate_zero(int i, int occurrences[]){
while (i < 26) {
//if it is present only once
if (occurrences[i] == 0)
return i;
i++;
}
//if all are doubles or more
return -1;
}
//printing the modified string
string print_modified(string str) {
int n = str.length();
//if conversion
//not possible
if (n > 26)
return "-1";
string ch = str;
int i, occurrences[26] = {0};
//counting the occurrences
for (i = 0; i < n; i++)
occurrences[ch[i] - 'a']++;
int index = calculate_zero(0, occurrences);
for (i = 0; i < n; i++) {
//replacing the character
if (occurrences[ch[i] - 'a'] > 1) {
occurrences[ch[i] - 'a']--;
ch[i] = (char)('a' + index);
occurrences[index] = 1;
//moving to the next character
index = calculate_zero(index + 1, occurrences);
}
}
cout << ch << endl;
}
int main() {
string str = "tutorialspoint";
print_modified(str);
} আউটপুট
bucdrealspoint