Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that simulates the search and replace operation in a text editor

ID: 3548333 • Letter: W

Question

Write a program that simulates the search and replace operation in a text editor. The program is to have only

three function calls in main. The first function prompts the user to type a string of less than 100 characters. It

then prompts the user to type the search-substring of 10 or fewer characters. Finally, it prompts the user to

type the replace-substring of 10 or fewer characters.

The second call is the search and replace function, which replaces all occurrences of the search-substring with

the replace-substring. Assume that the resulting string will not exceed 100 characters.

After the search and replace function returns, a print function prints the resulting string as a series of 80

character lines. It performs word-wrap. That is, a line can end only at a space. If there is no space in 80

characters, then print 79 characters and a hyphen and continue on the next line.

Explanation / Answer

#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. ", 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(" Math 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 == ' ' )

{

*tmpword='';

tmpword--;

if( compare(search_str, word) == 1 )

{

//printf(" match found.");

replaceText();

}

len = my_strlen(word);

for( count1=0; count1 < len; count1++)

{ *tmpword=' '; //*tmpword='';

tmpword--;

}

}

/* else if( *tmpword == ' ')

{

len = my_strlen(word);

for( count1=0; count1 < len; count1++)

{ *tmpword=' '; //*tmpword='';

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(" Invalid file name entered.");

getFileName(str);

}

/*

if( ferror(fop) != 0)

{ fclose(fop);

printf(" Some 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='';

fflush(NULL);

getstr = str;

while(1)

{

scanf("%c", &readch);

if( readch == ' ')

break;

*getstr = readch;


//if( ( str = (char *)realloc(str, sizeof(char)) ) == '')// break;

count++;

getstr++;

if( count >= 20)

break;

}

*getstr='';

}


int my_strlen(char *str)

{

int _tmpval = 0;

char *cstr = str;


if(*cstr != '')

{

while(*cstr !='')

{

_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 == '' || *str=='' )

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.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote