Created
May 19, 2015 10:03
-
-
Save amcsi/6068ef6ae59951ed4a9f to your computer and use it in GitHub Desktop.
My str_replace() implementation in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author amcsi / Szerémi Attila | |
**/ | |
#include <stdio.h> | |
/** | |
* Returns the length of a string | |
**/ | |
int strlen(char* str) { | |
int ret = 0; | |
while (str[ret]) { | |
ret++; | |
} | |
return ret; | |
} | |
/** | |
* Replaces all found instances of the passed substring in the passed string. | |
* | |
* @param search The substring to look for | |
* @param replace The substring with which to replace the found substrings | |
* @param subject The string in which to look | |
* | |
* @return A new string with the search/replacement performed | |
**/ | |
char* str_replace(char* search, char* replace, char* subject) { | |
int i, j, k; | |
int searchSize = strlen(search); | |
int replaceSize = strlen(replace); | |
int size = strlen(subject); | |
char* ret; | |
if (!searchSize) { | |
ret = malloc(size + 1); | |
for (i = 0; i <= size; i++) { | |
ret[i] = subject[i]; | |
} | |
return ret; | |
} | |
int retAllocSize = (strlen(subject) + 1) * 2; // Allocation size of the return string. | |
// let the allocation size be twice as that of the subject initially | |
ret = malloc(retAllocSize); | |
int bufferSize = 0; // Found characters buffer counter | |
char* foundBuffer = malloc(searchSize); // Found character bugger | |
for (i = 0, j = 0; i <= size; i++) { | |
/** | |
* Double the size of the allocated space if it's possible for us to surpass it | |
**/ | |
if (retAllocSize <= j + replaceSize) { | |
retAllocSize *= 2; | |
ret = (char*) realloc(ret, retAllocSize); | |
} | |
/** | |
* If there is a hit in characters of the substring, let's add it to the | |
* character buffer | |
**/ | |
else if (subject[i] == search[bufferSize]) { | |
foundBuffer[bufferSize] = subject[i]; | |
bufferSize++; | |
/** | |
* If the found character's bugger's counter has reached the searched substring's | |
* length, then there's a hit. Let's copy the replace substring's characters | |
* onto the return string. | |
**/ | |
if (bufferSize == searchSize) { | |
bufferSize = 0; | |
for (k = 0; k < replaceSize; k++) { | |
ret[j++] = replace[k]; | |
} | |
} | |
} | |
/** | |
* If the character is a miss, let's put everything back from the buffer | |
* to the return string, and set the found character buffer counter to 0. | |
**/ | |
else { | |
for (k = 0; k < bufferSize; k++) { | |
ret[j++] = foundBuffer[k]; | |
} | |
bufferSize = 0; | |
/** | |
* Add the current character in the subject string to the return string. | |
**/ | |
ret[j++] = subject[i]; | |
} | |
} | |
/** | |
* Free memory | |
**/ | |
free(foundBuffer); | |
return ret; | |
} | |
int main(argc, argv) { | |
char* origText = "How much woodwood would a woodchuck chuck if a woodchuck could chuck wood?"; | |
char* replace = "fluttershyraritytwilightsparkleapplejackrainbowfashpinkiepie"; | |
//replace = "XXX"; | |
char* result = str_replace("wood", replace, origText); | |
printf(result); | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment