Inspect Replacing Example


Scenario - Replacing the specific characters in COBOL program.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. INSPECTR.
       AUTHOR. MTH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.

       01 WS-DATA    PIC X(50) VALUE
             "COBOL LANGUAGE IS A HIGH LEVEL LANGUAGE".

       PROCEDURE DIVISION.
           DISPLAY "BEFORE REPLACING:  " WS-DATA.
           INSPECT WS-DATA
                   REPLACING ALL SPACES BY "#" AFTER "LEVEL"
                            "LANGUAGE" BY "&&&&&&&&" BEFORE "HIGH" 
                            "IS" BY "**".
           DISPLAY "AFTER REPLACING:   " WS-DATA.
           STOP RUN.

Output -

BEFORE REPLACING:  COBOL LANGUAGE IS A HIGH LEVEL LANGUAGE           
AFTER REPLACING:   COBOL &&&&&&&& ** A HIGH LEVEL#LANGUAGE###########

Explaining Example -

In the above case,

  • SPACES AFTER "LEVEL" replaced by "#".
  • "LANGUAGE" before "HIGH" replaced by "&&&&&&&&".
  • "IS" replaced by "**".

After replacements, the result is "COBOL &&&&&&&& ** A HIGH LEVEL#LANGUAGE###########".