برنامج C لاستبدال كلمة في النص بكلمة أخرى

بالنظر إلى ثلاث سلاسل "str" ​​و"oldW" و"newW". المهمة هي العثور على جميع تكرارات الكلمة "oldW" واستبدالها بالكلمة "newW". أمثلة:

Input : str[] = 'xxforxx xx for xx' oldW[] = 'xx' newW[] = 'geeks' Output : geeksforgeeks geeks for geeks 
موصى به: يرجى حلها يمارس أولاً قبل الانتقال إلى الحل.

تتمثل الفكرة في اجتياز السلسلة الأصلية وحساب عدد مرات ظهور الكلمة القديمة في السلسلة. الآن أنشئ سلسلة جديدة بحجم كافٍ حتى يمكن استبدال الكلمة الجديدة. الآن انسخ السلسلة الأصلية إلى سلسلة جديدة مع استبدال الكلمة. 

تطبيق:

C
   // C program to search and replace    // all occurrences of a word with    // other word.    #include            #include            #include            // Function to replace a string with another    // string    char  *     replaceWord  (  const     char  *     s       const     char  *     oldW           const     char  *     newW  )      {         char  *     result  ;         int     i       cnt     =     0  ;         int     newWlen     =     strlen  (  newW  );         int     oldWlen     =     strlen  (  oldW  );         // Counting the number of times old word       // occur in the string       for     (  i     =     0  ;     s  [  i  ]     !=     ''  ;     i  ++  )     {         if     (  strstr  (  &  s  [  i  ]     oldW  )     ==     &  s  [  i  ])     {         cnt  ++  ;         // Jumping to index after the old word.       i     +=     oldWlen     -     1  ;         }         }         // Making new string of enough length       result     =     (  char  *  )  malloc  (  i     +     cnt     *     (  newWlen     -     oldWlen  )     +     1  );         i     =     0  ;         while     (  *  s  )     {         // compare the substring with the result       if     (  strstr  (  s       oldW  )     ==     s  )     {         strcpy  (  &  result  [  i  ]     newW  );         i     +=     newWlen  ;         s     +=     oldWlen  ;         }         else      result  [  i  ++  ]     =     *  s  ++  ;         }         result  [  i  ]     =     ''  ;         return     result  ;      }      // Driver Program    int     main  ()      {         char     str  []     =     'xxforxx xx for xx'  ;         char     c  []     =     'xx'  ;         char     d  []     =     'Geeks'  ;         char  *     result     =     NULL  ;         // oldW string       printf  (  'Old string: %s  n  '       str  );         result     =     replaceWord  (  str       c       d  );         printf  (  'New String: %s  n  '       result  );         free  (  result  );         return     0  ;      }      
الإخراج:
Old string: xxforxx xx for xx New String: GeeksforGeeks Geeks for Geeks 

تعقيد الوقت : على)
المساحة المساعدة: O(n)

الطريقة الثانية: تتضمن هذه الطريقة التحديث الموضعي للسلسلة. إنه أكثر كفاءة لأنه يستخدم مساحة إضافية فقط لإدراج الأحرف الجديدة. 

تطبيق:

C
   // C Program to replace a word in a text by another given   // word by inplace updation   #include         #include         #include         void     replaceWord  (  char  *     str       char  *     oldWord       char  *     newWord  )   {      char     *  pos       temp  [  1000  ];      int     index     =     0  ;      int     owlen  ;      owlen     =     strlen  (  oldWord  );      // Repeat This loop until all occurrences are replaced.      while     ((  pos     =     strstr  (  str       oldWord  ))     !=     NULL  )     {      // Bakup current line      strcpy  (  temp       str  );      // Index of current found word      index     =     pos     -     str  ;      // Terminate str after word found index      str  [  index  ]     =     ''  ;      // Concatenate str with new word      strcat  (  str       newWord  );      // Concatenate str with remaining words after      // oldword found index.      strcat  (  str       temp     +     index     +     owlen  );      }   }   int     main  ()   {      char     str  [  1000  ]     oldWord  [  100  ]     newWord  [  100  ];      printf  (  'Enter the string: '  );      gets  (  str  );      printf  (  'Enter the word to be replaced: '  );      gets  (  oldWord  );      printf  (  'Replace with: '  );      gets  (  newWord  );      replaceWord  (  str       oldWord       newWord  );      printf  (  '  n  Modified string: %s'       str  );      return     0  ;   }   
مدخل:
1 xxforxx xx for xx xx geeks 
الإخراج:
geeksforgeeks geeks for geeks 

تعقيد الوقت: O(n)
المساحة المساعدة: O(1)

إنشاء اختبار