-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from shumin1027/escape_character
escape character
- Loading branch information
Showing
4 changed files
with
63 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// Created by shumin on 2019/10/29. | ||
// | ||
|
||
#include <string.h> | ||
#include <stdlib.h> | ||
|
||
char *strreplace(char const *const original, | ||
char const *const pattern, char const *const replacement) { | ||
size_t const replen = strlen(replacement); | ||
size_t const patlen = strlen(pattern); | ||
size_t const orilen = strlen(original); | ||
|
||
size_t patcnt = 0; | ||
const char *oriptr; | ||
const char *patloc; | ||
|
||
// find how many times the pattern occurs in the original string | ||
for (oriptr = original; (patloc = strstr(oriptr, pattern)); oriptr = patloc + patlen) { | ||
patcnt++; | ||
} | ||
|
||
{ | ||
// allocate memory for the new string | ||
size_t const retlen = orilen + patcnt * (replen - patlen); | ||
char *const returned = (char *) malloc(sizeof(char) * (retlen + 1)); | ||
|
||
if (returned != NULL) { | ||
// copy the original string, | ||
// replacing all the instances of the pattern | ||
char *retptr = returned; | ||
for (oriptr = original; (patloc = strstr(oriptr, pattern)); oriptr = patloc + patlen) { | ||
size_t const skplen = patloc - oriptr; | ||
// copy the section until the occurence of the pattern | ||
strncpy(retptr, oriptr, skplen); | ||
retptr += skplen; | ||
// copy the replacement | ||
strncpy(retptr, replacement, replen); | ||
retptr += replen; | ||
} | ||
// copy the rest of the string. | ||
strcpy(retptr, oriptr); | ||
} | ||
return returned; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// | ||
// Created by shumin on 2019/10/29. | ||
// | ||
|
||
char *strreplace(char const *const original, | ||
char const *const pattern, char const *const replacement); |