Replacing Characters with COBOL's Inspect Keyword

Aug 19, 2024

Using Inspect Keyword in COBOL

Introduction

  • Today's session topic: Replacing characters using the Inspect keyword.
  • Previous sessions discussed using the Inspect keyword to count occurrences of characters.

Purpose of Inspect Keyword

  • Inspect keyword is a statement that examines and modifies strings.
  • Today's focus: Replacing specific characters in a string with other specific characters.

Important Points

  • Such questions may appear in interviews.
  • It is necessary to review previous sessions to clarify the basic knowledge.

Identification Division

  • Program ID: Name of the program.
  • Author's name.
  • Comment line:
    • Replace all occurrences of a character.

Data Division

  • The data division includes storage and working-storage sections.
  • Variable declaration:
    • 01 data name and variable name.
    • PIC: Alpha numeric 40 characters.
    • Value clause: Optional and used as needed.

Requirement

  • All characters should be converted to capital letters.
  • Two small 't' characters should be replaced with capital 'T'.

Procedure Division

  • Main logic of the program.
  • Use of the Inspect keyword:
    • Inspect STR replacing all small t's by capital T's.
    • Displays modified string.

Conclusion

  • The code is simple and clear.
  • Program termination: Stop run.
  • The output was seen after compiling and running the program.
  • Useful in real-life scenarios.

Further Discussion

  • Other important topics will be discussed in the next sessions.

These notes are based on using the Inspect keyword in COBOL and explain the process of character replacement in a string in a simple way.