Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Find and Replace function

Posted By: Benjamin Evans     Category: C Programming     Views: 10715

Write a program to search and/or replace a word in C Program. It asks for an input file and 2 string. One for searching and one for replacement. If the file contains search string than it will print that line containing search string and asks for the confirmation to change it. If you say "yes" it changes it else continues till the file is over.

Code for Find and Replace function in C Programming

#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>

void getString(char *str);
int my_strlen(char *str);
int compare(char *source, char *str);

void getFileName(char *str);
void getSearchStr(char *str);
void getReplaceStr(char *str);
void startSearch();
void replaceText();


char *word;
char *filename;
char *search_str;
char *replace_str;
char *tmpname = "chin.XYZ";

FILE *fop, *tpfile;

  void main()
  {
     int count = 0;
     clrscr();

     word = (char *)malloc(sizeof(char) * 20);
     filename = (char *)malloc(sizeof(char) * 20);
     search_str = (char *)malloc(sizeof(char) * 20);
     replace_str = (char *)malloc(sizeof(char) * 20);

     getFileName(filename);
     getSearchStr(search_str);
     getReplaceStr(replace_str);
     startSearch();
/*
while( feof(fop) == 0)
{
fseek(fop,count,0);
c = getc(fop);
printf("%c",c);
count = count + 1L;
}
*/
fclose(fop); fclose(tpfile); if( remove(filename) == 0) { if (rename( tmpname, filename) == 0) printf("Renamed %s to %s.\n", tmpname, filename); else printf("Cannot renamed. %d", count); } getch(); } void replaceText() { int len=0, rplen=0, count=0 ; char chr; //char *tmpword;char *rplword; long i_fptr=0; //tmpword = word; rplword = replace_str; len = my_strlen(word); rplen = my_strlen(replace_str); fflush(NULL); printf("\nMath found. Do you want to replace it 'y' for yes and 'n' for no. : "); scanf("%c", &chr); if( chr == 'y' || chr == 'Y') { i_fptr = ftell(tpfile); fseek(tpfile,i_fptr - len-1, 0); // fseek(fop,i_fptr - len-1, 0);for(count=0; count < rplen; count++) { putc(*rplword,tpfile); // putc(*rplword,fop); rplword++; } putc(' ',tpfile); //putc(' ', fop);//fseek(tpfile, i_fptr +( rplen+1-len), 1); } } void startSearch() { //long count=0;int len=0, count1=0; char *tmpword=word; tpfile = fopen(tmpname, "w+t"); while( feof(fop) == 0) { //fseek(fop,count,0); *tmpword = getc(fop); putc(*tmpword, tpfile); if( isspace(*tmpword) > 0 || *tmpword == '.' || *tmpword == '\n' ) { *tmpword='\0'; tmpword--; if( compare(search_str, word) == 1 ) { //printf("\n match found."); replaceText(); } len = my_strlen(word); for( count1=0; count1 < len; count1++) { *tmpword=' '; //*tmpword='\0'; tmpword--; } } /* else if( *tmpword == '\n')
{
len = my_strlen(word);
for( count1=0; count1 < len; count1++)
{ *tmpword=' '; //*tmpword='\0';
tmpword--;
}
}
*/
tmpword++; //printf("%c",c);//count = count + 1L; } } void getFileName(char *str) { printf("Enter File name maxium 20 character : "); getString(filename); if( ( fop = fopen(filename, "r+t") ) == NULL ) { fclose(fop); printf("\nInvalid file name entered."); getFileName(str); } /*
if( ferror(fop) != 0)
{ fclose(fop);
printf("\nSome error has occured. Please enter another file name.");
getFileName(str);
}
*/
} void getSearchStr(char *str) { printf("Enter search string of maxium 20 character : "); getString(str); } void getReplaceStr(char *str) { printf("Enter replace string of maxium 20 character : "); getString(str); } void getString(char *str) { int count=0; char *getstr; char readch='\0'; fflush(NULL); getstr = str; while(1) { scanf("%c", &readch); if( readch == '\n') break; *getstr = readch; //if( ( str = (char *)realloc(str, sizeof(char)) ) == '\0')// break; count++; getstr++; if( count >= 20) break; } *getstr='\0'; } int my_strlen(char *str) { int _tmpval = 0; char *cstr = str; if(*cstr != '\0') { while(*cstr !='\0') { _tmpval++; cstr++; } } return _tmpval; } int compare(char *source, char *str) { int cflag = 0; char *tsource = source; while(1) { if( *tsource == *str) cflag = 1; else { cflag = 0; break; } tsource++; str++; if( *tsource == '\0' || *str=='\0' ) break; } return cflag; } /************************ Input ****************************/
Content Of Input File text.dat is hello thisis find and replace examples. This is only for testing you cannot use this program commerically. Enter File name maxium 20 character : text.dat Invalid file name entered.Enter File name maxium 20 character : output.txt Enter search string of maxium 20 character : this Enter replace string of maxium 20 character : there Math found. Do you want to replace it 'y'for yes and 'n'for no. : y Math found. Do you want to replace it 'y'for yes and 'n'for no. : y Math found. Do you want to replace it 'y'for yes and 'n'for no. : y Renamed chin.XYZ to output.txt. /************************ Output ****************************/
hello there is find and replace examples. This is only for testing you cannot use there program commerically.
  
Share: 


Didn't find what you were looking for? Find more on Find and Replace function Or get search suggestion and latest updates.

Benjamin Evans
Benjamin Evans author of Find and Replace function is from London, United Kingdom.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!