Напишите программу, которая находит все экземпляры указанного шаблона в данной строке и заменяет их другой строкой.Базовая строка, шаблон поиска и строка замены будут предоставлены пользователем.Я написал такую программу, но что-то не так с ней, и я не мог понять, что.
input:
enter the sentence: hello world
enter the word which u want to replace: world
enter the new word: hello
output:
The word appears one times
#define SIZE 80
void newsentence(char a[], char b[], char c[], int n, int k);
void array(int f, int s, int g, char buff[], char b[], char c[], int n, int k);
int main()
{
char a[SIZE], b[SIZE], c[SIZE];
int k;
printf("Enter the sentence: ");
gets(a);
printf("\nEnter the word which u want to replace: ");
scanf("%s",b);
printf("\nEnter the new word: ");
scanf("%s",c);
int n =0;
char *temp;
temp =a;
while((temp =strstr(temp,b))!= NULL){
n++;
temp++;
k = strstr(temp,b)-b;
}
printf("The word appears %d times",n);
newsentence(a,b,c,n,k);
}
void newsentence(char a[], char b[], char c[], int n, int k)
{
int f, s, g;
char buff;
f=strlen(a);
s=strlen(b);
g=strlen(c);
strcpy(buff,a);
array(f,s,g, buff,b,c,n,k);
}
void array(int f, int s, int g, char buff[], char b[], char c[], int n, int k)
{
int i,j;
int q;
q = k +s - 1;
for(j = 0; k<=q ;k++, j++)
{
if(strcmp(buff[k],b[j])==0){
strcmp(buff[k],c[i]);
}
}
for(i = 0; i<SIZE;i++){
printf("Yout new string is: %c",buff[i]);
}
}