现在的位置: 首页 > 综合 > 正文

strrep()函数的实现

2017年12月27日 ⁄ 综合 ⁄ 共 1696字 ⁄ 字号 评论关闭

char* strrep(const char* src,  const char* from, const char* to)
{

}


#include <string.h>
#include 
<malloc.h>

char* strrep(const char* src,  const char* from, const char* to)
{
  
char *des, *des_cur;
  
int from_len, to_len, src_len, des_len;
  
const char *src_cur, *src_end, *occ;
  
const char **marks; 
  
int mark_len, m, rep_count;
  
// prepare
  from_len = strlen(from);
  to_len 
= strlen(to);
  src_len 
= strlen(src);
  
if (from_len == 0)
    
return strdup(src);
  
// mark all occurence of `from' in `src'
  mark_len = 0x4;
  marks 
= (const char**)malloc(sizeof(char** mark_len);
  
if (marks == NULL)
    
return NULL;
  rep_count 
= 0;
  src_cur 
= src;
  
while((occ = strstr(src_cur, from)) != NULL) {
    rep_count
++;
    
// need more space for mark
    if (rep_count > mark_len) {
      mark_len 
<< 1;
      marks 
= (const char**)realloc(marks, mark_len);
      
if (marks == NULL)
        
return NULL;
    }
    
// mark the position
    marks[rep_count - 1= occ;
    
// find next occurence from the current position
    src_cur = occ + from_len;
  }
  
// construct new string
  des_len = src_len + (to_len - from_len) * rep_count;
  des 
= (char*)malloc(des_len + 1);
  
if (des == NULL)
    
return NULL;
  des_cur 
= des;
  src_cur 
= src;
  m 
= 0;
  
if (m < rep_count)
    occ 
= marks[m];
  
else
    occ 
= NULL;
  
while(*src_cur) {
    
if (src_cur != occ)
      
*des_cur++ = *src_cur++;
    
else {
      
// replace `from' with `to'
      strncpy(des_cur, to, to_len);
      src_cur 
+= from_len;
      des_cur 
+= to_len;
      
// more to replace?
      m++;
      
if (m < rep_count)
        occ 
= marks[m];
      
else
        occ 
= NULL;
    }
  }
  des_cur 
= '\0';
  free(marks);
  
return des;
}

int main()
{
        char m[10] = "abc";
        printf("%s",strrep(m,m,m));
        return 0;
}

抱歉!评论已关闭.