【发布时间】:2017-03-17 09:00:37
【问题描述】:
这个函数的目的是用它的回文创建一个字符串concat。例如 abc -> abccba
这是我的代码,结果仍然显示原始字符串,没有任何变化。我为字符串及其回文保留了一些空格,但它仍然不起作用。
char *mirror(const char *str) {
char *result = malloc(2 * strlen(str) * sizeof(char));
int str_len = strlen(str);
for (int i = 0; i < str_len; ++i) {
result[i] = str[i];
}
for (int j = str_len; j < 2*str_len; ++j) {
result[j] = str[2*str_len-j];
}
return result;
}
【问题讨论】:
标签: memory malloc palindrome