C: implementing str_replace

Articles may may have files attached at the end of the post

And example on how to implement str_replace in C.

This implementation will only replace the value once. If the substring to replace is not found, a copy of the original string is returned.

If you are looking for a version that replace all occurrences of substring, please have a look at str_replace all matching pattern.

  1. /**
  2.  * vim: tabstop=2:shiftwidth=2:softtabstop=2:expandtab
  3.  *
  4.  * str_replace.c implements a str_replace PHP like function
  5.  * Copyright (C) 2009  chantra <chantra__A__debuntu__D__org>
  6.  *
  7.  * This program is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU General Public License
  9.  * as published by the Free Software Foundation; either version 2
  10.  * of the License, or (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  20.  *
  21.  * gcc -o str_replace str_replace.c
  22.  */
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26.  
  27.  
  28. void usage(char *p){
  29.   fprintf(stderr, "USAGE: %s string tok replacement\n", p );
  30. }
  31.  
  32. /*
  33.  * Create a new string with [substr] being replaced by [replacement] in [string]
  34.  * Returns the new string, or NULL if out of memory.
  35.  * The caller is responsible for freeing this new string.
  36.  */
  37. char *
  38. str_replace( const char *string, const char *substr, const char *replacement ){
  39.   char *tok = NULL;
  40.   char *newstr = NULL;
  41.  
  42.   tok = strstr( string, substr );
  43.   if( tok == NULL ) return strdup( string );
  44.   newstr = malloc( strlen( string ) - strlen( substr ) + strlen( replacement ) + 1 );
  45.   if( newstr == NULL ) return NULL;
  46.   memcpy( newstr, string, tok - string );
  47.   memcpy( newstr + (tok - string), replacement, strlen( replacement ) );
  48.   memcpy( newstr + (tok - string) + strlen( replacement ), tok + strlen( substr ), strlen( string ) - strlen( substr ) - ( tok - string ) );
  49.   memset( newstr + strlen( string ) - strlen( substr ) + strlen( replacement ), 0, 1 );
  50.   return newstr;
  51. }
  52.  
  53. int main( int argc, char **argv ){
  54.  
  55.   char *ns = NULL;
  56.   if( argc != 4 ) {
  57.     usage(argv[0]);
  58.     return 1;
  59.   }
  60.   ns = str_replace( argv[1], argv[2], argv[3] );
  61.   fprintf( stdout, "Old string: %s\nTok: %s\nReplacement: %s\nNew string: %s\n", argv[1], argv[2], argv[3], ns );
  62.   free(ns);
  63.   return 0;
  64. }

Will output:

$ gcc -o str_replace str_replace.c
$ ./str_replace "(uid=%u)" "%u" chantra
Old string: (uid=%u)
Tok: %u
Replacement: chantra
New string: (uid=chantra)
$ ./str_replace "(uid=%u)" "%p" chantra
Old string: (uid=%u)
Tok: %p
Replacement: chantra
New string: (uid=%u)

AttachmentSize
str_replace.c2.23 KB