OneCompiler

Abhishek Mishra(problem - 1)

160

#include <bits/stdc++.h>

using namespace std;

int main()
{
string mystr1; // = "is my is name is john is";
cout<<"Enter the sentence: " << endl;
getline(cin, mystr1);

string mystr2; //= "is";
cout<<"Enter the word you want to remove : " <<endl;
cin>> mystr2;

int i = 0;
int j = mystr1.length();

int x = 0;
int y = mystr2.length();

string temp = "";
int flag=0;

string newstr = "";

while(i<j)
{
   if(flag == 0)
   {
       if(mystr1[i] == mystr2[0])
       {
          temp += mystr1[i];
          flag = 1;
          x++;
       }
       else newstr += mystr1[i];
     
   }
   
  else if(flag == 1)
   {
       if(mystr1[i] == mystr2[x])
       {
          temp += mystr1[i];
          x++;
       }
       else
       {
         newstr += mystr1[i];
         if(!x == y) newstr += temp;
         temp = "";
         flag =  0;
         x = 0;
       }
    
   }

 i++; 
}
cout<<newstr;
return 0;

}