Answer the question
In order to leave comments, you need to log in
How to replace words in a file?
Write a program that replaces one word in a text file with another. A word is a sequence of non-whitespace characters, delimited by spaces or line breaks. The pattern word may begin with either a capital or a lowercase letter; replacement must be appropriate. If the pattern word matches part of a word, no replacement is performed. A word may be followed by a punctuation mark from the following set: ".,:;!?". The original text is written in the input.txt file, the processed text must be output to the output.txt file.
Specifications Input
The first line of the input file input.txt contains the sample word, the second line contains the replacement word. These words contain only lowercase letters. The following lines contain the text to be processed.
Output
The program should output the processed text to the output.txt file.
Examples
input
tail
head
Bobtail has a tail.
output
Bobtail has a head.
Can't find an error. Thanks in advance!
var i: integer; inp,out: text; s,s_o,s_z,s_z2,s_o2: string;
begin
assign(inp, 'input.txt');
assign(out, 'output.txt');
rewrite(out);
reset(inp);
readln(inp,s_o);
readln(inp,s_z);
s_o2:=s_o;
insert(' ',s_o,1);
s_o2[1]:=Upcase(s_o2[1]);
s_z2:=s_z;
insert(' ',s_z,1);
while not eof(inp) do
begin
readln(inp,s);
while pos(s_o,s)<>0 do
begin
i:=pos(s_o,s);
delete(s,i,length(s_o));
insert(s_z,s,i);
end;
while pos(s_o2,s)<>0 do
begin
i:=pos(s_o2,s);
delete(s,i,length(s_o2));
insert(s_z2,s,i);
end;
writeln(out,s);
end;
close(inp);
close(out);
end.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question