Skip to content

Instantly share code, notes, and snippets.

@sycobuny
Last active January 19, 2017 20:43
Show Gist options
  • Save sycobuny/d70a842df6366195b779f8bc728b1c99 to your computer and use it in GitHub Desktop.
Save sycobuny/d70a842df6366195b779f8bc728b1c99 to your computer and use it in GitHub Desktop.
A proof-of-concept for a simple string replacement program
Old string: Here's a string with a couple ' in it
New String: Here\'s a string with a couple \' in it
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "test.h"
int main(int argc, char *argv[])
{
char *oldstr = "Here's a string with a couple ' in it";
char *newstr = str_replace(oldstr, "'", "\\'");
printf("Old string: %s\n", oldstr);
printf("New String: %s\n", newstr);
}
char *str_replace(char *string, char *needle, char *replacement);
int substr_count(char *string, char *substring);
/**
* Replace all occurences of a substring with another substring
*
* This function will allocate memory resources sufficient to duplicate a
* string, with all occurences of a given substring replaced. Note this means
* that you have to `free()` the result of this function yourself.
*
* Author:
* Stephen Belcher
* Parameters:
* string: The original string to duplicate/replace matches on
* needle: The substring to search for to replace
* replacement: The substring to replace each occurence of `needle` with
* Returns:
* char*: A string allocated with `malloc()` that (free with `free()`), a
* duplicate of the `string` parameter with all instances of `needle`
* replaced by `replacement`.
**/
char *str_replace(char *string, char *needle, char *replacement)
{
// get the length of the search/replacement strings, we'll use these a lot
int nlen = strlen(needle);
int rlen = strlen(replacement);
// construct a new memory object to store the replacement string
char *new = (char *) malloc(
sizeof(char) *
// the number of characters, plus or minus the difference in size that
// exists between the string and the string after replacement occurs
strlen(string) + (
// replacement length minus needle length times occurrences
(rlen - nlen) * substr_count(string, needle)
)
);
char *nextpos = strstr(string, needle);
char *copypos = new;
char *lastpos = copypos;
while (nextpos) {
// copy the string up until the next needle
strncpy(copypos, string, nextpos - string);
// move the copy position up by how much we just copied
copypos += nextpos - string;
// copy in the replacement
strcpy(copypos, replacement);
// move the copy position up by the length of the replacement
copypos += rlen;
// move the string position up to where the nextpos was, plus the
// length of the needle
string = nextpos + nlen;
// find the next occurence of the needle
nextpos = strstr(string, needle);
}
// get the last bit of the string that was left after the final needle
strcpy(copypos, string);
return new;
}
/**
* Count the occurences of a substring inside of another string
*
* Parameters:
* string: The string containing occurences to count
* substring: The substring that is being counted
* Returns:
* The total number of times `substring` occurs inside of `string`
* References:
* Adapted from this Stackoverflow answer:
* http://stackoverflow.com/a/9052540
**/
int substr_count(char *string, char *substring)
{
int count = 0;
int substr_len = strlen(substring);
char *pos = strstr(string, substring);
while (pos)
{
count++;
pos = strstr(pos + substr_len, substring);
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment