C: implementing str_replace
Articles may may have files attached at the end of the post
Submitted by chantra on Wed, 10/07/2009 - 15:38
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.
- /**
- * vim: tabstop=2:shiftwidth=2:softtabstop=2:expandtab
- *
- * str_replace.c implements a str_replace PHP like function
- * Copyright (C) 2009 chantra <chantra__A__debuntu__D__org>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- * gcc -o str_replace str_replace.c
- */
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- void usage(char *p){
- fprintf(stderr, "USAGE: %s string tok replacement\n", p );
- }
- /*
- * Create a new string with [substr] being replaced by [replacement] in [string]
- * Returns the new string, or NULL if out of memory.
- * The caller is responsible for freeing this new string.
- */
- char *
- str_replace( const char *string, const char *substr, const char *replacement ){
- char *tok = NULL;
- char *newstr = NULL;
- tok = strstr( string, substr );
- if( tok == NULL ) return strdup( string );
- newstr = malloc( strlen( string ) - strlen( substr ) + strlen( replacement ) + 1 );
- if( newstr == NULL ) return NULL;
- memcpy( newstr, string, tok - string );
- memcpy( newstr + (tok - string), replacement, strlen( replacement ) );
- memcpy( newstr + (tok - string) + strlen( replacement ), tok + strlen( substr ), strlen( string ) - strlen( substr ) - ( tok - string ) );
- memset( newstr + strlen( string ) - strlen( substr ) + strlen( replacement ), 0, 1 );
- return newstr;
- }
- int main( int argc, char **argv ){
- char *ns = NULL;
- if( argc != 4 ) {
- usage(argv[0]);
- return 1;
- }
- ns = str_replace( argv[1], argv[2], argv[3] );
- fprintf( stdout, "Old string: %s\nTok: %s\nReplacement: %s\nNew string: %s\n", argv[1], argv[2], argv[3], ns );
- free(ns);
- return 0;
- }
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)
| Attachment | Size |
|---|---|
| str_replace.c | 2.23 KB |












