Computer >> कंप्यूटर >  >> प्रोग्रामिंग >> C++

लघु पाठ आकारों के लिए स्ट्रिंग खोज एल्गोरिथम को लागू करने के लिए C++ प्रोग्राम

इस C++ प्रोग्राम में इनपुट के रूप में एक टेक्स्ट और एक पैटर्न दिया जाता है। आउटपुट के रूप में, पैटर्न को टेक्स्ट में खोजा जाता है और पैटर्न के सभी उदाहरण दिए जाते हैं।

एल्गोरिदम

Begin
   Take the string and pattern as input.
   Declare the original and duplicate array with their size.
   Put the lengths of original and duplicate in len_ori and len_dupli.
   Make a loop for find out the position of the searched pattern.
   If pattern is not found, print not found otherwise print the no of instances of the searched pattern.
End

उदाहरण कोड

#include<iostream>
#include<cstring>
using namespace std;

int main() {
   char ori[120], dupli[120];
   int i, j, k = 0, len_ori, len_dupli;

   cout<<"enter string without any blank space"<<endl;

   cout<<"\nEnter Original String:";
   cin>>ori;

   cout<<"Enter Pattern to Search:";
   cin>>dupli;

   len_ori = strlen(ori);
   len_dupli = strlen(dupli);

   for (i = 0; i <= (len_ori - len_dupli); i++) // loop to find out the position Of searched pattern {
      for (j = 0; j < len_dupli; j++) {
         if (ori[i + j] != dupli[j])
         break ;
      }
      if (j == len_dupli) {
         k++;
         cout<<"\nPattern Found at Position: "<<i;
      }
   }
   if (k == 0)
      cout<<"\nNo Match Found!";
   else
      cout<<"\nTotal Instances Found = "<<k;
      return 0;
}

आउटपुट

enter string without any blank space

Enter Original String:Enter Pattern to Search:
Pattern Found at Position: 0
Pattern Found at Position: 1
Pattern Found at Position: 2
Pattern Found at Position: 3
Total Instances Found = 4

  1. जॉनसन के एल्गोरिथम को लागू करने के लिए C++ प्रोग्राम

    यहां हम दो शीर्षों के बीच सबसे छोटा रास्ता खोजने के लिए जॉनसन का एल्गोरिदम देखेंगे। ग्राफ यहां दिया गया है। किनारों के बीच सबसे छोटा रास्ता नीचे जैसा है। यह कार्यक्रम अपनी लागतों के साथ शीर्षों की संख्या, किनारों की संख्या और किनारों की संख्या लेगा। इनपुट - कार्यक्षेत्र:3 किनारे:5 लागत के स

  1. कडाने के एल्गोरिथम को लागू करने के लिए C++ प्रोग्राम

    कडाने के एल्गोरिथ्म का उपयोग पूर्णांकों की एक सरणी से अधिकतम सबअरे योग का पता लगाने के लिए किया जाता है। यहां हम इस एल्गोरिथम को लागू करने के लिए C++ प्रोग्राम पर चर्चा करेंगे। एल्गोरिदम Begin Function kadanes(int array[], int length):    Initialize    highestMax = 0    

  1. सी ++ प्रोग्राम विगेनियर साइफर को लागू करने के लिए

    Vigenere Cipher, अल्फ़ाबेटिक टेक्स्ट को एन्क्रिप्ट करने की एक तरह की पॉलीअल्फ़ाबेटिक प्रतिस्थापन विधि है। इस विधि में एन्क्रिप्शन और डिक्रिप्शन के लिए Vigenere Cipher Table का उपयोग किया जाता है जिसमें A से Z तक के अक्षर 26 पंक्तियों में लिखे जाते हैं। एन्क्रिप्शन कुंजी: स्वागत है संदेश: यहिस्ट