C Find Replace Program

Download here. string_replace.pdf

// Filename : string_replace.c

// Author : A.G.Raja

// Website : agraja.wordpress.com

// License : GPL

#include <stdio.h>

 

 

char* string_replace(unsigned char *from, char *find, char *replace)

{

const char *p;

char *q, *to;

int i,d, match;

to=malloc(sizeof(char)*strlen(from)*strlen(replace)/strlen(find));

 

for(p = from, q = to; *p != ”; p++)

{

// find logic

for(d=0;d<strlen(find);d++)

{

if (*(p+d) == *(find+d)) match=1;

else match =0;

}

// replace logic

if (match)

{

for(i=0;i<strlen(replace);i++)

{

*(q+i)=*(replace+i);

}

q=q+i; p=p+d-1;

}

else // copy unchanged chars

{ *q=*p; q++; }

 

} *q = ”;

return to;

}

 

 

 

 

 

 

 

 

 

 

// Test code

int main()

{

unsigned char *str = “this is original text” ;

char f[]=”original”;

char r[]=”new”;

unsigned char *str2;

printf(“before:%s\n”,str);

str2=string_replace(str,f,r);

printf(“after:%s\n”,str2);

return 0;

}

 

Download here. string_replace.pdf

Tips:

See the following pages for char replace, trim spaces

char replace

trim spaces

 

About these ads

5 thoughts on “C Find Replace Program

  1. Pingback: C Find and Replace program « Applied Electronics Journal

  2. Pingback: C program to trim blank spaces « Applied Electronics Journal

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s