首先,我正在尝试创建一个程序,该程序采用两个字符数组并从第一个数组中删除第二个数组中出现的任何字符。我添加了评论,以便更容易理解正在发生的事情。
#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;
}
这是输出:
而不是 'Post: I' 它应该是: 'Post: I at fd' 总而言之,我得到了错误的输出,我目前不知道如何修复它。