How could I write a function to take in the following:
filename: (a string that corresponds to the name of a file)
wordA and wordB: They are both two strings with no space

The function should do this:
A- read the a txt file line by line
B- replace every occurrence of wordA to wordB.
C- Write the modifified text file with the same as the original file, but preprended with 'new\_'. For instance, if the input file name was 'data.txt', the output would be 'new\_data.txt'.

Here is what I have done. It has so many mistakes but I got the main idea. Could you please help to find my mistake and to make the function work.

```
function [ ] = replaceStr( filename,wordA, wordB )
% to replace wordA to wordB in a txt and then save it in a new file.

newfile=['new_',filename]
fh=fopen(filename, 'r')
fh1=fgets(fh)
fh2=fopen(newfile,'w')
line=''
while ischar(line)
    line=fgetl(fh)
    newLine=[]
while ~isempty(line)
    [word line]= strtok(line, ' ')
if strcmp(wordA,wordB)
word=wordB
      end
newLine=[ newLine word '']
end
newLine=[]
fprintf('fh2,newLine')
end

fclose(fh)
fclose(fh2)

end

```