Replace by X
PROBLEM :
Given a string and a pattern, Replace all the continuous occurrence of pattern with a single X in the string. For a clear view see the example below.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is string str.
The second line of each test case contains a string s,which is a pattern.
Output:
Print the modified string str.
Constraints:
1 = T = 100
1 = size of str,s = 1000
Example:
Input
2
abababcdefababcdab
ab
geeksforgeeks
geeks
Output
XcdefXcdX
XforX
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
#include<malloc.h>
char* Replace_by_X(char *,char *) ;
int compare(char *,char *,int ) ;
int main()
{
int t ;
char *str,*patt ;
str=(char*)malloc(1000*sizeof(char)) ;
patt=(char*)malloc(1000*sizeof(char)) ;
cin>>t ;
while(t--)
{
cin>>str>>patt ;
str=Replace_by_X(str,patt) ;
cout<<str<<endl ;
}
return 0;
}
char* Replace_by_X(char *str,char *patt)
{
char *ans ;
ans=(char*)malloc(1000*sizeof(char)) ;
int l1,l2,k,i ;
l1=strlen(str) ;
l2=strlen(patt) ;
k=0 ;
for(i=0;i<l1;i++)
{
if((str[i]==patt[0])&&(compare(str,patt,i)))
{
if(ans[k-1]!='X')
ans[k++]='X' ;
i=i+l2-1 ;
}
else
ans[k++]=str[i] ;
}
ans[k]='\0' ;
return ans ;
}
int compare(char * str,char * patt,int pos)
{
int l=strlen(patt);
int i ;
for(i=0;i<l;i++)
{
if(patt[i]!=str[pos+i])
return 0 ;
}
return 1 ;
}
---------------------------------------------------------------------------------
Given a string and a pattern, Replace all the continuous occurrence of pattern with a single X in the string. For a clear view see the example below.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is string str.
The second line of each test case contains a string s,which is a pattern.
Output:
Print the modified string str.
Constraints:
1 = T = 100
1 = size of str,s = 1000
Example:
Input
2
abababcdefababcdab
ab
geeksforgeeks
geeks
Output
XcdefXcdX
XforX
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
#include<malloc.h>
char* Replace_by_X(char *,char *) ;
int compare(char *,char *,int ) ;
int main()
{
int t ;
char *str,*patt ;
str=(char*)malloc(1000*sizeof(char)) ;
patt=(char*)malloc(1000*sizeof(char)) ;
cin>>t ;
while(t--)
{
cin>>str>>patt ;
str=Replace_by_X(str,patt) ;
cout<<str<<endl ;
}
return 0;
}
char* Replace_by_X(char *str,char *patt)
{
char *ans ;
ans=(char*)malloc(1000*sizeof(char)) ;
int l1,l2,k,i ;
l1=strlen(str) ;
l2=strlen(patt) ;
k=0 ;
for(i=0;i<l1;i++)
{
if((str[i]==patt[0])&&(compare(str,patt,i)))
{
if(ans[k-1]!='X')
ans[k++]='X' ;
i=i+l2-1 ;
}
else
ans[k++]=str[i] ;
}
ans[k]='\0' ;
return ans ;
}
int compare(char * str,char * patt,int pos)
{
int l=strlen(patt);
int i ;
for(i=0;i<l;i++)
{
if(patt[i]!=str[pos+i])
return 0 ;
}
return 1 ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment