<分区>
我试图在不使用任何额外缓冲区的情况下从字符串中删除重复字符。当我像这样声明单个变量时,代码有效
char s[] = "aaabbb";
但当我试图循环通过一些测试用例时不是。
#include <stdio.h>
#include <string.h>
/* Design an algorithm and write code to remove the duplicate characters in a string
without using any additional buffer. NOTE: One or two additional variables are fine.
An extra copy of the array is not. */
void removeDuplicates(char s[]) {
// attempts to modify array in place without extra buffer
}
int main() {
char *s[4] = {"aaaa", "abcd", "ababab", "aaabbb"};
int i;
for (i = 0; i < 6; i++) {
removeDuplicates(s[i]);
}
return 0;
}
这会返回 Bus error: 10
因为它试图修改字符串文字 "aaaa"
但我不确定如何在保持良好设置的同时克服这个问题测试用例。