Sha256: a5e2ceccc6b7182142917e831f03a56ea8e8bbad38be527575881f3b84db1304
Contents?: true
Size: 1.13 KB
Versions: 3
Compression:
Stored size: 1.13 KB
Contents
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "string_extras.h" void remove_last_char(char* str) { size_t len = strlen(str); str[len - 1] = '\0'; } void remove_first_char(char* str) { size_t len = strlen(str); memmove(str, str + 1, len); } char * str_replace (char *string, const char *substr, const char *replacement) { char *tok = NULL; char *newstr = NULL; char *oldstr = NULL; newstr = strdup(string); while ( ( tok = strstr( newstr, substr ) ) ) { oldstr = newstr; newstr = malloc ( strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) + 1 ); /* If failed to alloc mem, free old string and return NULL */ if ( newstr == NULL ) { free (oldstr); return NULL; } memcpy ( newstr, oldstr, tok - oldstr ); memcpy ( newstr + (tok - oldstr), replacement, strlen ( replacement ) ); memcpy ( newstr + (tok - oldstr) + strlen( replacement ), tok + strlen ( substr ), strlen ( oldstr ) - strlen ( substr ) - ( tok - oldstr ) ); memset ( newstr + strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) , 0, 1 ); free (oldstr); } return newstr; }
Version data entries
3 entries across 3 versions & 1 rubygems