#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int countOccurrence(char* sen,char* word)
{
	int count=0,i,k,len1,len2;
	
	len1=strlen(sen);
	len2=strlen(word);
	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;

	len1=strlen(sen);
	len2=strlen(oldword);
	len3=strlen(newword);

	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';

	strcpy(sen,ptr);
	//free(ptr);
}

int main()
{
	char sen[50],oldword[10],newword[10];

	fgets(sen,50,stdin);
	fgets(oldword,10,stdin);
	fgets(newword,10,stdin);

	replace(sen,oldword,newword);

	printf("Modified string:\n%s",sen);

	return 0;
}
