Home > Back-end >  Don't call library functions to string substitution problem
Don't call library functions to string substitution problem

Time:10-02

A job, through the subject to good function prototype structure function, but I always have a little problem, write can help take a look at where not
Int str_replace (const char * in, char * out, int outlen, const char * oldstr, const char * newstr)
{
Int len_in=0;//the string pointer
Int len_out=0;//string pointer now
Int counts=0;//replace number
Int len_new=0;//the new string length
For (; * (newstr + len_new)!='\ 0'; Len_new + +)
;//new string length

Char STR [outlen];//output string array

While (len_out & lt; Outlen - 1) {
If (* (in + len_in)=='\ 0')
break;//the string end point to the empty time
If (* (in + len_in)!=* oldstr | | len_out + len_new & gt;=outlen - 1) {//search less than to replace string or output length does not allow to be replaced
STR [len_out]=* (in + len_in);
Len_in + +;
Len_out + +;
continue;
} else {
int i;
For (I=1; * (oldstr + I)!='\ 0'; I++)
If (* (in + len_in + I)! + I)=* (oldstr)
break;//figure out whether the following a few characters matching
If (* (oldstr + I)=='\ 0') {//to finish the replacement string is matched, can replace
Counts++;
Len_in +=I;//ignore replaced part
For (int j=0; J & lt; Len_new; J++) {
STR [len_out + +]=* (newstr + j);
}
} else {
STR [len_out]=* (in + len_in);
Len_in + +;
Len_out + +;
}
}
}
STR [len_out]='\ 0';
Out=STR;
Return counts;
}

No way, to the main function is to look at the reference how to implement this function, after the return to replace, and modify the out Pointers, I this writing may also face a timeout
  • Related