INSPECT REPLACING
Part of the Tables and Text section of Coddy's COBOL Programming journey. Lesson 14 of 19.
INSPECT ... REPLACING changes characters in the field itself:
INSPECT PHONE-IN REPLACING ALL "." BY " ".ALL replaces every match, FIRST only the first, and LEADING the run at the start:
01 AMT PIC X(8) VALUE "00012300".
INSPECT AMT REPLACING LEADING "0" BY " ".
*> AMT is now " 12300"The text found and its replacement must be the same size. REPLACING ALL "ON" BY "X" stops the compiler:
error: REPLACING operands differ in sizeCONVERTING maps characters one to one: each character of the first string becomes the one in the same place in the second.
INSPECT CODE-IN CONVERTING "abc" TO "ABC".Challenge
EasyThe program reads a date like 2026/11/23 into DATE-IN. Replace every / with -, so it prints 2026-11-23.
Example input, one line per ACCEPT: 2026/11/23.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. DASHES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 DATE-IN PIC X(10).
PROCEDURE DIVISION.
ACCEPT DATE-IN.
*> Replace the slashes here
DISPLAY DATE-IN.
STOP RUN.