#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();
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);
{
putc(*rplword,tpfile);
rplword++;
}
putc(' ',tpfile);
}
}
void startSearch()
{
char *tmpword=word;
tpfile = fopen(tmpname, "w+t");
while( feof(fop) == 0)
{
*tmpword = getc(fop);
putc(*tmpword, tpfile);
if( isspace(*tmpword) > 0 || *tmpword == '.' || *tmpword == '\n' )
{
*tmpword='\0';
tmpword--;
if( compare(search_str, word) == 1 )
{
replaceText();
}
len = my_strlen(word);
for( count1=0; count1 < len; count1++)
{
*tmpword=' ';
tmpword--;
}
}
tmpword++;
}
}
void getFileName(char *str)
{
printf("Enter File name maximum 20 character : ");
getString(filename);
if( ( fop = fopen(filename, "r+t") ) == NULL )
{
fclose(fop);
printf("\nInvalid file name entered.");
getFileName(str);
}
}
void getSearchStr(char *str)
{
printf("Enter search string of maximum 20 character : ");
getString(str);
}
void getReplaceStr(char *str)
{
printf("Enter replace string of maximum 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;
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 this is find and replace examples.
This is only for testing you cannot use this
program commercially.
Enter File name maximum 20 character : text.dat
Enter search string of maximum 20 character : this
Enter replace string of maximum 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
Renamed chin.XYZ to text.dat
/************************ Output ****************************/
hello there is find and replace examples.
This is only for testing you cannot use there
program commercially.