Code written in C language functions ReplaceAll

Some of the content development process often used to be a collection, the following information about the content ReplaceAll functions written in C language.

#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;
}







Guess you like

Origin www.cnblogs.com/s-squirrel/p/11242614.html
Recommended