Firstly, I'm trying to create a program which takes two char arrays and removes from the first any chars that shew up in the second array. I have added comments to make it easier to understand what's happening.
#include <stdio.h>
void rcfs(char s[], char t[]){
int i=0, j=0;
while (t[i] != '\0')
++i; /*find size of t*/
for (int x=0;x<i;++x){ /*go through both vals of x and
check vals of s to see if they equal a val of x, if so set j of s to 0*/
while (s[j]!=0){
if (s[j]==t[x]){
s[j]=0;
}
j++;
}
if (x<i-1)
j=0;
}
char new[8]; /*new char arr to be assigned to s*/
int x=0; //seperate counter that increments only when chars are added to new
for (int q=0;q<j;q++){
if (s[q]!=0)
new[x++]=s[q];
}
new[7]=0;
s=new; //maybe these need be pointers
}
int main(){
char s[]="I eat food";
char t[]="oe";
printf("Pre: %s\n", s);
rcfs(s, t);
printf("Post: %s", s);
return 0;
}
Here is the output:
Instead of
'Post: I' it should be:
'Post: I at fd'
To summarise, I'm getting the wrong output and I'm currently unaware of how to fix it.