কম্পিউটার

C++ এ বিকল্প স্বরবর্ণ এবং ব্যঞ্জনবর্ণ স্ট্রিং


একটি প্রদত্ত স্ট্রিংয়ের ক্ষেত্রে, প্রদত্ত স্ট্রিংয়ের অক্ষরগুলিকে পুনর্বিন্যাস করুন যাতে স্বরবর্ণ এবং ব্যঞ্জনবর্ণগুলি বিকল্প অবস্থান দখল করে। যদি স্ট্রিং সঠিকভাবে পুনরায় সাজানো না যায়, তাহলে "এমন কোনো স্ট্রিং নেই" প্রদর্শন করুন। একে অপরের প্রতি সম্মানের সাথে স্বরবর্ণের ক্রম এবং একে অপরের প্রতি সম্মানের সাথে ব্যঞ্জনবর্ণের ক্রম সংরক্ষণ করা উচিত।

যদি একাধিক প্রয়োজনীয় স্ট্রিং তৈরি করা যায় তবে অভিধানিকভাবে ছোট প্রদর্শন করুন।

উদাহরণ

Input : Tutorial
Output : Tutorila
Input : onse
Output : nose

দুটি সম্ভাব্য ফলাফল "নাক" এবং "একটি" বিদ্যমান। যেহেতু "নাক" আভিধানিকভাবে ছোট তাই আমরা এটি প্রদর্শন করি৷

  • প্রদত্ত স্ট্রিংয়ে স্বরবর্ণ এবং ব্যঞ্জনবর্ণের সংখ্যা গণনা করা হয়।

  • এই ক্ষেত্রে, গণনার মধ্যে পার্থক্য একাধিক হলে, "সম্ভব নয়" ফেরত দিন।

  • এই ক্ষেত্রে, ব্যঞ্জনবর্ণের চেয়ে বেশি স্বর থাকলে, প্রথম স্বরবর্ণ প্রথমে প্রদর্শন করুন এবং অবশিষ্ট স্ট্রিংয়ের জন্য পুনরাবৃত্তি করুন।

  • এই ক্ষেত্রে, যদি স্বরবর্ণের চেয়ে বেশি ব্যঞ্জনবর্ণ থাকে, তবে প্রথম ব্যঞ্জনবর্ণটি প্রথমে প্রদর্শন করুন এবং অবশিষ্ট স্ট্রিংয়ের জন্য পুনরাবৃত্তি করুন।

  • এই ক্ষেত্রে, গণনা একই হলে, প্রথম ব্যঞ্জনবর্ণের সাথে প্রথম স্বরবর্ণের তুলনা করুন এবং প্রথমে ছোটটি প্রদর্শন করুন।

উদাহরণ

// C++ application of alternate vowel and consonant string
#include <bits/stdc++.h>
using namespace std;
// 'ch1' is treated as vowel or not
bool isVowel(char ch1){
   if (ch1 == 'a' || ch1 == 'e' || ch1 == 'i' ||
   ch1 == 'o' || ch1 =='u')
   return true;
   return false;
}
// build alternate vowel and consonant string
// str1[0...l2-1] and str2[start...l3-1]
string createAltStr(string str1, string str2,
int start1, int l1){
   string finalStr1 = "";
   // first adding character of vowel/consonant
   // then adding character of consonant/vowel
   for (int i=0, j=start1; j<l1; i++, j++)
   finalStr1 = (finalStr1 + str1.at(i)) + str2.at(j);
   return finalStr1;
}
// function to locate or find the needed alternate vowel and consonant string
string findAltStr(string str3){
   int nv1 = 0, nc1 = 0;
   string vstr1 = "", cstr1 = "";
   int l1 = str3.size();
   for (int i=0; i<l1; i++){
      char ch1 = str3.at(i);
      // count vowels and updaye vowel string
      if (isVowel(ch1)){
      nv1++;
      vstr1 = vstr1 + ch1;
   }
   // counting consonants and updating consonant string
   else{
         nc1++;
         cstr1 = cstr1 + ch1;
      }
   }
   // no such string can be built
   if (abs(nv1-nc1) >= 2)
   return "no such string";
   // delete first character of vowel string
   // then built alternate string with
   // cstr1[0...nc1-1] and vstr1[1...nv1-1]
   if (nv1 > nc1)
   return (vstr1.at(0) + createAltStr(cstr1, vstr1, 1, nv1));
   // delete first character of consonant string
   // then built alternate string with
   // vstr1[0...nv1-1] and cstr1[1...nc1-1]
   if (nc1 > nv1)
   return (cstr1.at(0) + createAltStr(vstr1, cstr1, 1, nc1));
   // if both vowel and consonant
   // strings are of equal length
   // start building string with consonant
   if (cstr1.at(0) < vstr1.at(0))
   return createAltStr(cstr1, vstr1, 0, nv1);
   // start building string with vowel
   return createAltStr(vstr1, cstr1, 0, nc1);
}
 // Driver program to test above
int main(){
   string str3 = "Tutorial";
   cout<< findAltStr(str3);
   return 0;
}

আউটপুট

Tutorila

সময়ের জটিলতা &মাইনাস O(n), যেখানে 'n' কে স্ট্রিং এর দৈর্ঘ্য হিসাবে ধরা হয়


  1. স্ট্রিংকে নম্বরে রূপান্তরিত করা হচ্ছে এবং C++-এ এর বিপরীতে

  2. একটি অক্ষর স্বর বা ব্যঞ্জনবর্ণ কিনা তা পরীক্ষা করার জন্য C++ প্রোগ্রাম

  3. C এবং C++ এ স্ট্রিং লিটারেলের ধরন কি?

  4. C++ স্ট্রিং ধ্রুবক এবং অক্ষর ধ্রুবকের মধ্যে পার্থক্য