需求描述
假设 一个 字符串 "abcdefg", 如 要从 第二个 开始( 索引 为 1), 删除 两个 字符, 则删除 后的 字符串 是" adefg"。
代码实现
int removeSubString(char * str, int index, int size){
if(NULL == str || index < 0 || size < 0){
printf("addBigInt param error\n");
return PARAM_ERR;
}
char * p = NULL, *t = NULL;
int len = strlen(str);
p = str + index;
t = p + size;
memcpy(p, t, len - index - size);
str[len - size] = '\0';
return SUCCESS;
}
void testremoveSubString(void){
char str[100] = "abcdefg";
printf("\n************ testremoveSubString ************ \n");
removeSubString(str, 1, 2);
printf("after remove sub stirng is : %s\n", str);
return;
}
代码编译
gcc main.c str.c -g -o a.exe
调试输出
************ testremoveSubString ************
after remove sub stirng is : adefg