#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int countOccurrence(char* sen,char* word)
{
int count=0,i,k,len1,len2;
for(i=0;i<len1-len2+1;)
{
k=0;
while(word[k] && sen[k+i]==word[k])
k++;
if(k==len2 && sen[k+i]==' ' || sen[k+i]=='\0')
{
count++;
i+=len2;
}
else ++i;
}
return count;
}
void replace(char* sen,char* oldword,char* newword)
{
int count,len1,len2,len3,i,top=-1,k;
char *ptr;
count=countOccurrence(sen,oldword);
if(!count) return;
ptr
=(char*)malloc(sizeof(len1
+count
*(len3
-len2
)+1));
for(i=0;i<len1-len2+1;)
{
k=0;
while(oldword[k] && sen[k+i]==oldword[k])
k++;
if(k==len2 && sen[k+i]==' ' || sen[k+i]=='\0')
{
for(k=0;newword[k];++k)
ptr[++top]=newword[k];
i+=len2;
}
else
{
ptr[++top]=sen[i];
++i;
}
}
ptr[++top]='\0';
}
int main()
{
char sen[50],oldword[10],newword[10];
replace(sen,oldword,newword);
printf("Modified string:\n%s",sen
);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgoKaW50IGNvdW50T2NjdXJyZW5jZShjaGFyKiBzZW4sY2hhciogd29yZCkKewoJaW50IGNvdW50PTAsaSxrLGxlbjEsbGVuMjsKCQoJbGVuMT1zdHJsZW4oc2VuKTsKCWxlbjI9c3RybGVuKHdvcmQpOwoJZm9yKGk9MDtpPGxlbjEtbGVuMisxOykKCXsKCQlrPTA7CgkJd2hpbGUod29yZFtrXSAmJiBzZW5baytpXT09d29yZFtrXSkKCQkJaysrOwoJCWlmKGs9PWxlbjIgJiYgc2VuW2sraV09PScgJyB8fCBzZW5baytpXT09J1wwJykKCQl7CgkJCWNvdW50Kys7CgkJCWkrPWxlbjI7CgkJfQoJCWVsc2UgKytpOwoJfQoJcmV0dXJuIGNvdW50Owp9Cgp2b2lkIHJlcGxhY2UoY2hhciogc2VuLGNoYXIqIG9s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