Replace a particular word of a sentence with another word
#include<stdio.h>
#include<string.h>
//replace a particular word in a sentence with another word
int main()
{
char str[100],strn[100],word[15],wrd[15],wrd1[15];
int j,i,k;
printf("Enter the sentence : ");
gets(str);
printf("Enter the word to be replaced : ");
gets(wrd);
printf("Enter the word to be replaced with : ");
gets(wrd1);
k=0;
for(i=0;str[i];i++)
{
j=0;
while(str[i]!=' ' && str[i]!='\0')
{
word[j++]=str[i++];
}
word[j]='\0';
if(strcmp(word,wrd)==0)
{
j=0;
while(wrd1[j]!='\0')
strn[k++]=wrd1[j++];
strn[k++]=' ';
}
else
{
j=0;
while(word[j]!='\0')
strn[k++]=word[j++];
strn[k++]=' ';
}
}
strn[--k]='\0';
printf("The new senetence is %s\n\n",strn);
}
I/O
#include<string.h>
//replace a particular word in a sentence with another word
int main()
{
char str[100],strn[100],word[15],wrd[15],wrd1[15];
int j,i,k;
printf("Enter the sentence : ");
gets(str);
printf("Enter the word to be replaced : ");
gets(wrd);
printf("Enter the word to be replaced with : ");
gets(wrd1);
k=0;
for(i=0;str[i];i++)
{
j=0;
while(str[i]!=' ' && str[i]!='\0')
{
word[j++]=str[i++];
}
word[j]='\0';
if(strcmp(word,wrd)==0)
{
j=0;
while(wrd1[j]!='\0')
strn[k++]=wrd1[j++];
strn[k++]=' ';
}
else
{
j=0;
while(word[j]!='\0')
strn[k++]=word[j++];
strn[k++]=' ';
}
}
strn[--k]='\0';
printf("The new senetence is %s\n\n",strn);
}
I/O
Comments
Post a Comment