为什么我会收到总线错误?有问题的行在代码中被标记。
练习 2-4。编写一个替代版本的squeeze(s1,s2),删除s1 中与字符串s2 中的任何字符匹配的每个字符。
#include <stdio.h>
/*
* Detects if a char is inside a string
*/
char in_string(char c, char s[]) {
int i = 0;
while (s[i] != '\0') {
if (s[i++] == c)
return 1;
}
return 0;
}
/*
* Returns the string s without any chars that are in map
*/
void squeeze(char s[], char map[]) {
int i, j;
for (i = j = 0; s[i] != '\0'; i++) {
if (! in_string(s[i], map)) {
s[j++] = s[i]; // <--- Bus Error
}
}
s[j] = '\0';
printf("%s\n", s);
}
main() {
squeeze("XALOMR", "AO");
squeeze("EWRTOG", "RGV");
}