Write a program to replace a particular word by another word in a given string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[80]={'\0'},b[20]={'\0'},c[20]={'\0'},d[20]={'\0'},e[50]={'\0'},f[50]={'\0'};
int i,j,k=0,h;
printf("Please Enter a String:= ");
gets(a);
printf("Please Enter the Serch String:= ");
gets(b);
printf("Please Enter the Replace String:= ");
gets(c);
for(i=0;i<=strlen(a);i++)
{
//Assuming Space , Tab and NULL as word separator
h=0;
if(a[i]==32 || a[i]=='\t' || a[i]=='\0')
{
d[k]='\0';
if(strcmp(b,d)==0)
{
for(j=i;j<=strlen(a);j++)
{
e[h]=a[j];
h++;
}
for(j=0;j<i-strlen(b);j++)
f[j]=a[j];
strcat(f,c);
strcat(f,e);
strcpy(a,f);
for(j=0;j<50;j++)
{
f[j]=NULL;
e[j]=NULL;
}
}
k=0;
}
else
{
d[k]=a[i];
k++;
}
}
puts(a);
getch();
}
#include<conio.h>
#include<string.h>
main()
{
char a[80]={'\0'},b[20]={'\0'},c[20]={'\0'},d[20]={'\0'},e[50]={'\0'},f[50]={'\0'};
int i,j,k=0,h;
printf("Please Enter a String:= ");
gets(a);
printf("Please Enter the Serch String:= ");
gets(b);
printf("Please Enter the Replace String:= ");
gets(c);
for(i=0;i<=strlen(a);i++)
{
//Assuming Space , Tab and NULL as word separator
h=0;
if(a[i]==32 || a[i]=='\t' || a[i]=='\0')
{
d[k]='\0';
if(strcmp(b,d)==0)
{
for(j=i;j<=strlen(a);j++)
{
e[h]=a[j];
h++;
}
for(j=0;j<i-strlen(b);j++)
f[j]=a[j];
strcat(f,c);
strcat(f,e);
strcpy(a,f);
for(j=0;j<50;j++)
{
f[j]=NULL;
e[j]=NULL;
}
}
k=0;
}
else
{
d[k]=a[i];
k++;
}
}
puts(a);
getch();
}