In: Computer Science
Write in C programming language
Write the function replace(char b[], char f[], char t[]). which finds the string 'f' in the string 'b' and replaces it with the string 't'. You can assume that f and t same length
Example:
char string[] = "zap";
replace(string, "ap", "oo");
--changes 'string' to "zoo".
*don't assume substring being replaced is
singular, or that its own substrings are unique.
*Don't re scan letters already checked and
replaced
char string[] =
"bsissisip";
replace(string, "sis",
"iss");
--should change 'string' to "bississip" (don't miss second
"sis")
#include<stdio.h>
#include<string.h>
void replaceSubstring(char [],char[],char[]);
main()
{
char string[100],sub[100],new_str[100];
printf("\nEnter a string: ");
gets(string);
printf("\nEnter the substring: ");
gets(sub);
#include<stdio.h>
#include<string.h>
void replace(char [],char[],char[]);
main()
{
char string[100],sub[100],new_str[100];
printf("\nEnter a string: ");
gets(string);
printf("\nEnter the substring: ");
gets(sub);
printf("\nEnter the new substring: ");
gets(new_str);
replaceSubstring(string,sub,new_str);
printf("\nThe string after replacing : %s\n",string);
}
void replace(char string[],char sub[],char new_str[])
{
int stringLen,subLen,newLen;
int i=0,j,k;
int flag=0,start,end;
stringLen=strlen(string);
subLen=strlen(sub);
newLen=strlen(new_str);
for(i=0;i<stringLen;i++)
{
flag=0;
start=i;
for(j=0;string[i]==sub[j];j++,i++)
if(j==subLen-1)
flag=1;
end=i;
if(flag==0)
i-=j;
else
{
for(j=start;j<end;j++)
{
for(k=start;k<stringLen;k++)
string[k]=string[k+1];
stringLen--;
i--;
}
for(j=start;j<start+newLen;j++)
{
for(k=stringLen;k>=j;k--)
string[k+1]=string[k];
string[j]=new_str[j-start];
stringLen++;
i++;
}
}
}
}
printf("\nEnter the new substring: ");
gets(new_str);
replaceSubstring(string,sub,new_str);
printf("\nThe string after replacing : %s\n",string);
}
void replaceSubstring(char string[],char sub[],char
new_str[])
{
int stringLen,subLen,newLen;
int i=0,j,k;
int flag=0,start,end;
stringLen=strlen(string);
subLen=strlen(sub);
newLen=strlen(new_str);
for(i=0;i<stringLen;i++)
{
flag=0;
start=i;
for(j=0;string[i]==sub[j];j++,i++)
if(j==subLen-1)
flag=1;
end=i;
if(flag==0)
i-=j;
else
{
for(j=start;j<end;j++)
{
for(k=start;k<stringLen;k++)
string[k]=string[k+1];
stringLen--;
i--;
}
for(j=start;j<start+newLen;j++)
{
for(k=stringLen;k>=j;k--)
string[k+1]=string[k];
string[j]=new_str[j-start];
stringLen++;
i++;
}
}
}
}
##replace variable string, sub and new_string with b,f,t Hope I am able to resolve your query,please like