×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Replace one or more word in a sentence by another word - String - C Language
2741    Arnab De    25/03/2020

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.

softetechnologies
#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();
}
softetechnologies
output
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Sri Sri Ramakrishna Paramahamsa
God is in all men, but all men are not in God; that is why we suffer.
Sri Sri Ramakrishna Paramahamsa
1007
58.53
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     44001
01/01/2018     36614
25/06/2018     35920
28/06/2017     34654
02/08/2017     33154
01/08/2017     27623
06/07/2017     27341
15/05/2017     26989
14/07/2017     22634
21/04/2018     21243
softetechnologies