I want to write a function that takes two character array parameters, say string1
and string2. Both string1 and string2 contain unknown number of
characters terminated by ’ !’. My function will erase every occurrence of
string2 in string1.
Example :
Your first string : abc!
Your second string : tabehwabcsje!
New version of your second string : tabehwsje
I was able to do that for a predefined number of characters but here in this program the string size is unknown and given by the user. This is what I did, any help would be appreciated!
( I wonder how Quzah will get angry for me this time:P!)
Code:
#include<stdio.h>
#define SIZE 50
void func(char [],int ,char [],int );
int main(void)
{
char string1[SIZE],string2[SIZE];
int i,c1=0,c2=0;
printf("Your first string, enter '!' after entering your string to terminate :\n");
scanf("%s",string1);
for(i=0;string1[i]!='!';i++){
c1++;//count element number of string1
}
printf("%d\n",c1);
printf("Your second string, add '!' again to the end of your string :\n");
scanf("%s",string2);
for(i=0;string2[i]!='!';i++){
c2++;//count element number of string2
}
printf("%d\n",c2);
func(string1,c1,string2,c2);
return 0;
}
void func(char a[],int c1,char b[],int c2){
int j=0,i=0,stop=0,count=0,k=0;
for(j=0;b[j]!='!';j++){//f1
if(b[j]==a[0]){//i1
for(i=0;a[i]!='!';i++){//f2
if(b[j]==a[i]){//i2
j++;
count++;
stop=0;
printf("olley\n");
}//i2
if(b[j]!=a[i]){
j++;
stop=1;
count=0;
printf("aaaa!\n");
}
if((count==c1)&&(stop==0)){//i3
for(k=0;k!=count;k++){//f3
b[j+count]=b[j];
j++; }//f3 }//i3
}//f2
}//i1
}//f1
printf("New version of your second string is:\n");
for(j=0;b[j]!='!';j++){
printf("%c",b[j]);
}
printf( "\n");
}