the question is :
Write a program that uses an array of pointers to strings str[ ]. Receive two strings str1 and str2 and check if str1 is embedded in any of the strings in str[ ]. If str1 is found, then replace it with str2.
Code:
char *str[ ] = {
"We will teach you how to...",
"Move a mountain",
"Level a building",
"Erase the past",
"Make a million",
"...all through C!"
} ;
For example if str1 contains "mountain" and str2 contains "car", then the second string in str should get changed to "Move a car".
Now i have made a program to compare and remove the whole string in str but how to compare particular word here i have no idea.just need the idea how to do that
my code :
Code:
/*30/10/13 22:15
*/
#include<stdio.h>
main( )
{
int i,a,x;
char *str[ ] = {
"We will teach you how to...",
"Move a mountain",
"Level a building",
"Erase the past",
"Make a million",
"...all through C!"
} ;
char str1[30],str2[30];
printf("Enter str1 :");
scanf("\n%[^\n]s",str1);
printf("Enter str2 :");
scanf("\n%[^\n]s",str2);
for(i=0;i<6;i++)
{
a=strcmp(str[i],str1);
if(a==0)
{
str[i]=str2;
}
}
for(x=0;x<6;x++)
printf("\n%s",str[x]);
}