我之前已经在Code Review中实施了一些建议。我还通过使用指针改进了我的代码。但是,下面的地址递增部分有什么问题squeezed_str++
?似乎地址没有增加。请指教。
PS。substring() 函数正在工作。:)
char *squeeze (char *str, int start_index, int end_index, char *ref_str) {
char *substr;
substr = malloc (sizeof (*substr));
if (substr == NULL) {
printf ("Unable to allocate memory.\n");
exit (EXIT_FAILURE);
}
char *squeezed_str;
squeezed_str = malloc (sizeof (*squeezed_str));
if (squeezed_str == NULL) {
printf ("Unable to allocate memory!\n");
exit (EXIT_FAILURE);
}
substr = substring (str, start_index, end_index);
int substr_len = strlen (substr);
int refstr_len = strlen (ref_str);
char chr1, chr2; chr1 = chr2 = '\0';
for (int i = 0; i < substr_len; i++) {
chr1 = *(substr+i);
for (int j = 0; j < refstr_len; j++) {
chr2 = *(ref_str + j);
if (chr1 == chr2) {
break;
}
}
if (chr1 != chr2) {
*squeezed_str = *(substr+i);
squeezed_str++;
}
}
return squeezed_str;
} /* end of squeeze() */