Replace one or more word in a sentence by another word in C Language.
25-03-2020
303 times

Write a Program to replace one or more occurrence of a word in a sentence by another word in C Language.
Read each word separately from the given sentence and compare to find the word. If it is equal then that word replaced by the given word otherwise restore the previous word. In last print the result.
#include<stdio.h> void main() { char str[100],word[50],findw[50],replacew[50],strres[100]; int i,j; strcpy(strres,""); printf("Enter String :"); gets(str); strcat(str," "); printf("Enter word for search: "); gets(findw); printf("Enter a word for replace: "); gets(replacew); j=0; for(i=0;str[i]!='\0';i++) { if(str[i]==' ') { word[j]='\0'; if(strcmp(word,findw)==0) { strcat(strres,replacew); } else { strcat(strres,word); } strcat(strres," "); j=0; } else { word[j++]=str[i]; } } printf(" Result is : "); puts(strres); getch(); }
