/ coding

C 算法例子

/*query="insert into chat_msg (msg) values ('#')";
*value="hello"
*compound_query(query,value);
*用 value 替换 query 中的 # 号
*结果如下
*insert into chat_msg (msg) values ('hello')
*/
char* compound_query(char* query,char* value){
  int value_length=strlen(value);
  int query_length=strlen(query);
  int move_length=0;
  static char tmp[100];
  memcpy(tmp,"0",sizeof(tmp));
  strcpy(tmp,query);
  for (int i = 0; i < query_length+value_length;)
  { 
    //从两边开始一起判断
    if(tmp[i]=='#' || tmp[query_length-1-i]=='#'){ 
      if(tmp[i]=='#'){
        //要移动的字符的长度
        move_length=query_length-1-i; 
      }else{
        move_length=i; 
      }
      //把#号后面的字符往后移value_length个长度
      for (int j = 0; j < move_length; ++j)
      {
        tmp[query_length+value_length-2-j]=tmp[query_length-1-j];
      }
      //末尾填充0
      tmp[query_length+value_length-1]=0x00;
      //用value的值填充空出的位置
      if(tmp[i]=='#'){
        for (int k  = 0; k < value_length; ++k){
          tmp[i+k]=value[k];
        }
      }else{
        for (int k  = 0; k < value_length; ++k){
          tmp[query_length-1-i+k]=value[k];
        }
      }
     //跳过value内部#号检测
     i=i+value_length-1;
    }
 i++;
 }
return tmp;
}