01 W-FILE2-STRING PIC X(whatever, your choice).
01 FILLER REDEFINES W-FILE2-STRING.
05 FILLER PIC X
OCCURS 1 TIMES
DEPENDING ON W-FILE2-SEARCH-BACKARDS-DISPLACEMENT.
05 W-FILE2-STRING-TEST-FOR-NON-BLANK PIC X.
01 W-FILE2-STRING-TO-SEARCH REDEFINES W-FILE2-STRING.
05 FILLER PIC X
OCCURS 1 TIMES
DEPENDING ON W-FILE2-STRING-LENGTH.
01 W-FILE2-DISPLACEMENT-OF-LAST-BYTE COMP PIC S9(4) VALUE ((whatever, your choice) minus 1).
01 W-FILE2-STRING-LENGTH COMP PIC S9(4).
MOVE your string from record on the second file to W-FILE2-STRING
MOVE W-FILE2-DISPLACEMENT-OF-LAST-BYTE TO W-FILE2-SEARCH-BACKARDS-DISPLACEMENT
Use a "loop construct" of your choice to reduce the value of W-FILE2-SEARCH-BACKARDS-DISPLACEMENT by 1 until
W-FILE2-STRING-TEST-FOR-NON-BLANK is not equal to space. Note, when your input field contains no trailing blanks, the "loop construct" should not execute. Note also, that if the field is entirely blank (ie
W-FILE2-SEARCH-BACKARDS-DISPLACEMENT becomes minus 1) then you have an entirely blank field and you should stop looking.
The length of the string on the second file, W-FILE2-STRING-LENGTH, can then be calculated as
(W-FILE2-SEARCH-BACKARDS-DISPLACEMENT + 1). Note, this will give you zero if the field is entirely blank.
Then we've finished with the second file.
For each record on the first file, first do a similar thing to above (all in new fields).
Then
Set a match flag off
if the two strings are zero length, set the match flag on, write the appropriate fourth file record and avoid the code to the end of the loop below
otherwise
if either of the strings are zero length, avoid the code to the end of the loop below (and will default to a non-match).
Now the guts of the thing. Some more data first.
01 FILLER REDEFINES W-FILE1-STRING.
05 FILLER PIC X
OCCURS 1 TIMES
DEPENDING ON W-FILE1-SEARCH-DISPLACEMENT.
05 W-FILE1-SUBSTRING.
10 FILLER PIC X
OCCURS 1 TIMES
DEPENDING ON W-FILE2-STRING-LENGTH. (yes, FILE2)
05 W-REMAINING-PART-OF-STRING.
10 FILLER PIC X
OCCURS 1 TIMES
DEPENDING ON W-FILE1-REMAINING-LENGTH.
01 FILLER REDEFINES W-FILE1-STRING.
05 FILLER PIC X
OCCURS 1 TIMES
DEPENDING ON W-FILE1-SEARCH-DISPLACEMENT.
05 W-SHUFFLE-OVER-SUBSTRING.
10 FILLER PIC X
OCCURS 1 TIMES
DEPENDING ON W-FILE1-SHUFFLE-LENGTH.
01 W-FILE1-REMAINING-LENGTH COMP PIC S9(4).
01 W-FILE1-SHUFFLE-LENGTH COMP PIC S9(4).
01 W-FILE1-LAST-DISPLACEMENT-TO-CHECK COMP PIC S9(4).
MOVE ZERO TO W-FILE1-SEARCH-DISPLACEMENT
Calculate W-FILE1-LAST-DISPLACEMENT-TO-CHECK as W-FILE1-LENGTH - W-FILE2-STRING-LENGTH.
MOVE W-FILE1-LENGTH TO W-FILE1-REMAINING-LENGTH
A loop construct, where W-FILE1-SEARCH-DISPLACEMENT is incremented as the last statement in the loop. The loop should not be entered when
W-FILE1-SEARCH-DISPLACEMENT is greater than W-FILE1-LAST-DISPLACEMENT-TO-CHECK.
inside the loop
if W-FILE2-STRING is equal to W-FILE1-SUBSTRING, set match flag, write appropriate record on fourth file
then calculate W-FILE1-SHUFFLE-LENGTH as W-FILE1-LENGTH - W-FILE1-SEARCH-DISPLACEMENT and W-FILE1-REMAINING-LENGTH as
W-FILE1-SHUFFLE-LENGTH - W-FILE2-STRING-LENGTH
then MOVE W-REMAINING-PART-OF-STRING TO W-SHUFFLE-OVER-SUBSTRING (which is longer, so will be padded with trailing spaces to blank out the end of the string that is the length of W-FILE2-STRING-LENGTH)
calculate W-FILE1-REMAINING-LENGTH as itself minus W-FILE2-STRING-LENGTH
otherwise calculate W-FILE1-REMAINING-LENGTH as itself minus 1 (the keeping-in-step of this is just for information and consistency, no effect on the logic)
end of the loop (remembering to increase W-FILE1-SEARCH-DISPLACEMENT as mentioned earlier)
test the match flag and write an appropriate record to the third file
When you've finished the first file's records, close everything that is still open and display the counts that you have been assiduously keeping.
That's about it.
WORDPAD again. Uncompiled, untested. Not even code, couldn't compile it. Haven't desk-checked it either.
This moves the "complexity" of the byte-by-byte approach from the procedural code to the data definition.
Once you've cracked it, you may find many uses for it. I've used similar types of data structures many times, although not usually from scratch over a long period of time.