Advertisement
ankit_saiyan

[C] Remove all the occurrences in String 1 of String 2 w/o any Library Function

May 6th, 2021 (edited)
959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 0 0
  1. /*******************************************************************************
  2.  
  3.     Remove all the occurrences of string 2 from string 1. For example -
  4.     ------Input
  5.     string 1 : abcxyababccz
  6.     string 2 : abc
  7.     ------Output
  8.     xyz
  9.     length: 3
  10.  
  11. *******************************************************************************/
  12.  
  13. #include <stdio.h>
  14.  
  15. // Function to find string length
  16. int length(char str[])
  17. {
  18.     int len;
  19.     for(len=0; str[len]!='\0'; len++);
  20.     return len;
  21. }
  22.  
  23. // Recursively removing string2 from string1
  24. void removeOccurrence(char str[], int i, int n)
  25. {
  26.     str[i] = str[i+n];
  27.  
  28.     if(str[i+n]=='\0')
  29.         return;
  30.     removeOccurrence(str, i+1, n);
  31. }
  32.  
  33. // function to find all the occurrences of string 2 in string 1
  34. void findOccurrence(char s1[], char s2[], int n1, int n2)
  35. {
  36.     int i=0, j=1, flag, pos;
  37.     while(i<n1)
  38.     {
  39.         /*
  40.             break if length to be compared of string 1 is
  41.             < the length of s2
  42.         */
  43.         if(n1-i < n2)
  44.             break;
  45.  
  46.         if(s1[i]==s2[0])
  47.         {
  48.             flag = 0;
  49.             for(j=1; j<n2; j++)
  50.             {
  51.                 if(s1[i+j]!=s2[j])
  52.                 {
  53.                     flag = 1;
  54.                     pos = j;    // storing the position where string didnt match
  55.                     break;
  56.                 }
  57.             }
  58.             // if flag is zero then s2 was found in s1 so remove it
  59.             if(flag==0)
  60.             {
  61.                 removeOccurrence(s1, i, n2);
  62.                 n1 = length(s1);            // Updating the length of string 1
  63.                 i = 0;
  64.             }
  65.             else        // s2 was not found in s1
  66.                 i += pos;
  67.         }
  68.         else
  69.             i++;
  70.     }
  71. }
  72.  
  73.  
  74. void main()
  75. {
  76.     char str1[100], str2[100];
  77.     int n1, n2;
  78.  
  79.     printf("Maximum String Length is 100!!!\n");
  80.     printf("Enter String 1: ");
  81.     gets(str1);
  82.     printf("Enter String 2: ");
  83.     gets(str2);
  84.  
  85.     n1 = length(str1);
  86.     n2 = length(str2);
  87.     // if n2 is greater then n1 cannot have occurrences of n2 in it
  88.     if(n1>n2)
  89.     {
  90.         findOccurrence(str1, str2, n1, n2);
  91.         printf("After removing Occurrences of String 2 from String 1\n");
  92.         n1 = length(str1);
  93.         printf("%s", str1);
  94.         printf("\nLength: %d", n1);
  95.     }
  96.     else
  97.         printf("Length of string 2 cannot be greater than that of String 1!\n");
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement