【发布时间】:2017-09-14 12:29:37
【问题描述】:
我需要制作一个接受字符串和字符的函数,该函数需要删除字符串中所有出现的字符并返回删除的字符总数。我已设法修改字符串以便能够删除不需要的字符,但我似乎无法用新字符串替换旧字符串。感谢您提前回复。
这是我到目前为止所管理的:
int clearstr(char *st,char u){
int i,j=0,total=0,size=strlen(st);
char *new_str=calloc(size,sizeof(char));
for(i=0;i<size;i++){
if(st[i]!=u){
new_str[j]=st[i];
j++;}
else total++;
}
new_str[j]='\0';
printf("%s",new_str);
//until here all is good ,new_str has the modified array that i want but i can't find a way to replace the string in st with the new string in new_str and send it back to the calling function (main),thanks for any help //
return total;
}
【问题讨论】:
-
new_str[j]='\0'; for(i=0;i