C Find and Replace program

Download here. find_replace.pdf

// Filename : find_replace.c

// Author : A.G.Raja

// Website : agraja.wordpress.com

// License : GPL

#include <stdio.h>

void find_replace(char *to, unsigned char *from, char find, char replace)

{

const char *p; char *q;

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

{

if ( *p != find)

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

else

{ *q=replace; q++; }

} *q = ”;

}

int main()

{

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

unsigned char str2[strlen(str)];

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

find_replace(str2,str,’s’,’x’);

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

return 0;

}

 

 

// This program can be used for single char only.

// string with more than one char is not supported

Download here. find_replace.pdf

Tips:

This function can be used to trim white spaces in file.

See this page for an example: Trim

See this page for string replace: string replace

 

 

 

 

Advertisements

3 thoughts on “C Find and Replace program

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

  2. Pingback: C Find Replace Program « 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