*>>SOURCE FORMAT IS FIXED *> *************************************************************** *> Author: *> Date: *> Purpose: *> Tectonics: cobc *> *************************************************************** identification division. program-id. test-replace. environment division. configuration section. input-output section. *> file-control. *> select *> assign to *> organization is *> . data division. *> file section. *> fd . *> 01 . working-storage section. 01 SUBSTITUTE-PARAMETERS. 05 SUB-BUFFER-SOURCE pic X(256). 05 SUB-BUFFER-TARGET pic X(320). 05 SUB-SEARCH-STRING pic X(48). 05 SUB-REPLACE-STRING pic X(48). 05 SUB-SEARCH-LENGTH pic 9(2). 05 SUB-REPLACE-LENGTH pic 9(2). local-storage section. linkage section. screen section. *> ************************************************************** procedure division. move "a string with & and another &" to SUB-BUFFER-SOURCE move "&" to SUB-SEARCH-STRING move 1 to SUB-SEARCH-LENGTH move "&" to SUB-REPLACE-STRING move 5 to SUB-REPLACE-LENGTH call "OCFR" using SUBSTITUTE-PARAMETERS display function trim(SUB-BUFFER-TARGET TRAILING) end-display goback. end program test-replace. *> *************************************************************** *>>SOURCE FORMAT IS FIXED *> *************************************************************** *> Author: *> Date: *> Purpose: *> Tectonics: cobc *> *************************************************************** identification division. program-id. OCFR. environment division. configuration section. input-output section. *> file-control. *> select *> assign to *> organization is *> . data division. *> file section. *> fd . *> 01 . working-storage section. 01 I1 pic 9(3) value 0. 01 I2 pic 9(3) value 0. 01 SUB-PTR pic 9(3) value 0. 01 SOURCE-SIZE pic 9(5) value 0. 01 TARGET-SIZE pic 9(5) value 0. local-storage section. linkage section. 01 SUBSTITUTE-PARAMETERS. 05 SUB-BUFFER-SOURCE pic X(256). 05 SUB-BUFFER-TARGET pic X(320). 05 SUB-SEARCH-STRING pic X(48). 05 SUB-REPLACE-STRING pic X(48). 05 SUB-SEARCH-LENGTH pic 9(2). 05 SUB-REPLACE-LENGTH pic 9(2). screen section. *> ************************************************************** procedure division using SUBSTITUTE-PARAMETERS. if SUB-SEARCH-LENGTH = SUB-REPLACE-LENGTH move SUB-BUFFER-SOURCE to SUB-BUFFER-TARGET inspect SUB-BUFFER-TARGET replacing all SUB-SEARCH-STRING(1:SUB-SEARCH-LENGTH) by SUB-REPLACE-STRING(1:SUB-REPLACE-LENGTH) else perform INSPECT-AND-REPLACE-EXTENDED end-if goback. INSPECT-AND-REPLACE-EXTENDED. add length of SUB-BUFFER-SOURCE to ZERO giving SOURCE-SIZE add length of SUB-BUFFER-TARGET to ZERO giving TARGET-SIZE move SPACES to SUB-BUFFER-TARGET subtract SUB-PTR from SUB-PTR inspect SUB-BUFFER-SOURCE tallying SUB-PTR for CHARACTERS before initial SUB-SEARCH-STRING(1:SUB-SEARCH-LENGTH) if SUB-PTR < SOURCE-SIZE - 1 add 1 to SUB-PTR giving I1 add 1 to SUB-PTR giving I2 move SPACES to SUB-BUFFER-TARGET move SUB-BUFFER-SOURCE(1:SUB-PTR) to SUB-BUFFER-TARGET move SUB-REPLACE-STRING(1:SUB-REPLACE-LENGTH) to SUB-BUFFER-TARGET(I2:SUB-REPLACE-LENGTH) add SUB-SEARCH-LENGTH to I1 add SUB-REPLACE-LENGTH to I2 move SUB-BUFFER-SOURCE(I1:SOURCE-SIZE - I1) to SUB-BUFFER-TARGET(I2:TARGET-SIZE - I2) else move SUB-BUFFER-SOURCE to SUB-BUFFER-TARGET end-if exit. goback. end program OCFR.