【发布时间】:2021-10-12 16:47:12
【问题描述】:
void strip_out(char *str) {
int length_string = strlen(str);
//printf("%d\n", length_string);
char temp[length_string];
int i = 0;
int j = 0;
while ((*(str + i) != '\0')) {
if (isalpha(str[i])) {
//printf("yes\n");
*(temp + j) = *(str + i);
i++;
j++;
}
else {
//printf("No\n");
i++;
}
}
我需要将我的 *str 值更改为 temp 的值,以便我可以在另一个函数中使用它。
我无法更改返回类型,如果可以的话,我可以只返回temp,但它必须是void。
【问题讨论】:
-
为什么不返回“新”字符串?
-
最简单的事情就是把
strcpy你的temp最后变成str。 -
另一种选择是循环之后的
strcpy(str, temp)。 -
比
*(temp + j)更常见的是temp[j] -
您可以就地进行转换。您只需要保留两个索引来指向您正在读取的位置和正在写入的位置。与您目前的逻辑几乎相同。
标签: c c-strings strip function-definition isalpha