Replace string or tokens as word from source string with given mode.
If 's' is given instead of 't'. Token string will be analyzed only one chunk word. So the replacement will be occured when the case of whole word matched. Second character is used to decide returning memory type and can be 'n' or 'r' which are stand on [n]ew and [r]eplace. When 'n' is given the result will be placed into new array so you should free the return string after using. Instead of this, you can also use 'r' character to modify source string directly. In this case, given source string should have enough space. Be sure that untouchable value can not be used for source string. So there are four associatable modes such like below. Mode "tn" : [t]oken replacing & putting the result into [n]ew array. Mode "tr" : [t]oken replacing & [r]eplace source string directly. Mode "sn" : [s]tring replacing & putting the result into [n]ew array. Mode "sr" : [s]tring replacing & [r]eplace source string directly.
char srcstr[256], *retstr; char mode[4][2+1] = {"tn", "tr", "sn", "sr"}; for(i = 0; i < 4; i++) { strcpy(srcstr, "Welcome to the qDecoder project."); printf("before %s : srcstr = %s\n", mode[i], srcstr); retstr = qStrReplace(mode[i], srcstr, "the", "_"); printf("after %s : srcstr = %s\n", mode[i], srcstr); printf(" retstr = %s\n\n", retstr); if(mode[i][1] == 'n') free(retstr); } --[Result]-- before tn : srcstr = Welcome to the qDecoder project. after tn : srcstr = Welcome to the qDecoder project. retstr = W_lcom_ _o ___ qD_cod_r proj_c_. before tr : srcstr = Welcome to the qDecoder project. after tr : srcstr = W_lcom_ _o ___ qD_cod_r proj_c_. retstr = W_lcom_ _o ___ qD_cod_r proj_c_. before sn : srcstr = Welcome to the qDecoder project. after sn : srcstr = Welcome to the qDecoder project. retstr = Welcome to _ qDecoder project. before sr : srcstr = Welcome to the qDecoder project. after sr : srcstr = Welcome to _ qDecoder project. retstr = Welcome to _ qDecoder project.
|