C语言编写的ReplaceAll函数的代码

2022-02-19  本文已影响0人  简简单单咦

将开发过程中经常用到的一些内容做个珍藏,下边资料是关于C语言编写的ReplaceAll函数的内容。

#include <stdio.h>

#include <malloc.h>

#include <string.h>

if(find == NULL || replaceWith == NULL){

return strdup(src);

}

int count = 0;

int i,j,k;

int srcLen = strlen(src);

int findLen = strlen(find);

int replaceWithLen = strlen(replaceWith);

int cpStrStart = 0;

count = getFindStrCount(src, find);

if(count == 0){

return strdup(src);

}

memset(afterReplaceHead, '0',sizeof(afterReplaceHead));

for(i = 0,j = 0,k = 0;i!=srcLen;i++){

if(src[i] == find[j]){

if(j == 0){

k = i;

}

if(j == (findLen-1)){

j = 0;

strncpy(afterReplaceIndex, srcIndex, i - findLen - cpStrStart + 1);

afterReplaceIndex = afterReplaceIndex + i - findLen - cpStrStart + 1;

srcIndex = srcIndex + i - findLen - cpStrStart + 1;

cpStrStart = i + 1;

strncpy(afterReplaceIndex, replaceWith, replaceWithLen);

afterReplaceIndex = afterReplaceIndex + replaceWithLen;

srcIndex = srcIndex + findLen;

}else{

j++;

}

}else{

if(j != 0){

i = k;

}

j = 0;

}

}

strncpy(afterReplaceIndex, srcIndex, i - cpStrStart);

return afterReplaceHead;

}

int count = 0;

int findLen = strlen(find);

while((position = strstr(position, find)) != NULL){

count++;

position = position + findLen;

}

return count;

}

int main(void){

printf("%sn",r);

free(r);

return 0;

}

上一篇下一篇

猜你喜欢

热点阅读