爲什麼我得到一個公交車錯誤?有問題的行在代碼中被標記。C - K&R練習2.4 - 爲什麼我會遇到巴士錯誤?
練習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");
}
的問題是在標題中所述及的問題的行標裏面的代碼。不過,我試圖修復的格式有問題。 – CamelCamelCamel