COBOL COBOL 2014 (ISO/IEC 1989:2014), built with GnuCOBOL 3.x ยท
โ verified by execution on 2026-08-16
The INSPECT statement is a powerful tool in COBOL that examines characters or groups of characters in a data item. Unlike modern languages that rely heavily on regular expressions or dynamic string manipulation methods, COBOL provides INSPECT as a built-in statement to count occurrences of characters, replace substrings, and convert sets of characters efficiently.
It is divided into several main clauses: TALLYING, REPLACING, and CONVERTING.
Counting Characters with TALLYING
The TALLYING phrase counts the occurrences of a specific character (alphanumeric, DBCS, or national) in a data item.
cobolโ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. TALLY-BASIC. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-TEXT PIC X(20) VALUE 'HELLO COBOL WORLD'. 01 WS-COUNT PIC 99 VALUE ZERO. PROCEDURE DIVISION. INSPECT WS-TEXT TALLYING WS-COUNT FOR ALL 'O'. DISPLAY 'Count of O: ' WS-COUNT. STOP RUN.
Output
Count of O: 04
Your output
A critical detail about TALLYING is that the INSPECT statement adds to the existing value of the tallying variable. It does NOT automatically reset the counter variable to zero before counting. If you run multiple INSPECT statements using the same counter without resetting it, the value will accumulate.
Predict the outputcobol
Read the code. What exactly will it print? Commit to an answer before you look.
IDENTIFICATION DIVISION. PROGRAM-ID. TALLY-ACCUM. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-TEXT-1 PIC X(10) VALUE 'APPLE'. 01 WS-TEXT-2 PIC X(10) VALUE 'BANANA'. 01 WS-A-COUNT PIC 99 VALUE ZERO. PROCEDURE DIVISION. INSPECT WS-TEXT-1 TALLYING WS-A-COUNT FOR ALL 'A'. INSPECT WS-TEXT-2 TALLYING WS-A-COUNT FOR ALL 'A'. DISPLAY 'Total As: ' WS-A-COUNT. STOP RUN.
Output
Total As: 04
You predicted
Replacing Text with REPLACING
The REPLACING phrase allows you to search for a specific sequence of characters and replace it. When identifier-1 is of usage DISPLAY, literals must be of category alphanumeric.
cobolโ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. REPL-BASIC. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-TEXT PIC X(20) VALUE 'HELLO COBOL WORLD'. PROCEDURE DIVISION. INSPECT WS-TEXT REPLACING ALL 'O' BY '*'. DISPLAY WS-TEXT. STOP RUN.
Output
HELL* C*B*L W*RLD
Your output
You can also specify that only the FIRST occurrence should be replaced:
cobolโ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. REPL-FIRST. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-TEXT PIC X(20) VALUE 'APPLE APPLE'. PROCEDURE DIVISION. INSPECT WS-TEXT REPLACING FIRST 'APPLE' BY 'PEACH'. DISPLAY WS-TEXT. STOP RUN.
Output
PEACH APPLE
Your output
Note: In standard COBOL, the search string and the replacement string in a REPLACING clause must be of the exact same length. You cannot replace 'APPLE' with 'BANANA' directly using INSPECT REPLACING.
Translating Characters with CONVERTING
While REPLACING looks for exact substring matches, CONVERTING works character-by-character. It converts all occurrences of specific characters in a data item to user-supplied replacement characters.
cobolโ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. CONVERT-BASIC. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-TEXT PIC X(20) VALUE 'HELLO COBOL WORLD'. PROCEDURE DIVISION. INSPECT WS-TEXT CONVERTING 'ABCDE' TO '12345'. DISPLAY WS-TEXT. STOP RUN.
Output
H5LLO 3O2OL WORL4
Your output
How CONVERTING maps characters
The CONVERTING statement creates a 1-to-1 character map based on the two strings provided.
Advanced Inspection Boundaries
You can use the BEFORE and AFTER phrases to restrict the inspection range. If the BEFORE or AFTER target is not found in the string, the BEFORE phrase scans to the end, while the AFTER phrase prevents any scanning.
cobolโ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. BEFORE-AFTER. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-TEXT PIC X(30) VALUE '[email protected]'. 01 WS-COUNT PIC 99 VALUE ZERO. PROCEDURE DIVISION. INSPECT WS-TEXT TALLYING WS-COUNT FOR CHARACTERS BEFORE INITIAL '@'. DISPLAY 'Length before @: ' WS-COUNT. STOP RUN.
Output
Length before @: 04
Your output
Finally, you can combine counting and replacing in a single pass. It counts the occurrences of specific characters and fills all or portions of a data item with specified characters.
cobolโ verified output
IDENTIFICATION DIVISION. PROGRAM-ID. TALLY-REPL. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-TEXT PIC X(20) VALUE '0001234'. 01 WS-COUNT PIC 99 VALUE ZERO. PROCEDURE DIVISION. INSPECT WS-TEXT TALLYING WS-COUNT FOR LEADING '0' REPLACING LEADING '0' BY '*'. DISPLAY 'Count: ' WS-COUNT ' Result: ' WS-TEXT. STOP RUN.
Output
Count: 03 Result: ***1234
Your output
The INSPECT statement replaces the need for custom character-scanning loops, keeping your code cleaner and fully taking advantage of native COBOL capabilities.
Check yourself
What happens to the tallying counter variable before an INSPECT TALLYING statement executes?
Reveal answer
It retains its previous value, and INSPECT adds to it. โ INSPECT adds to the existing value of the tallying variable. You must manually initialize the counter before the INSPECT statement.
Can the INSPECT ... REPLACING statement replace a word with a longer word (e.g., "CAT" with "MOUSE")?
Reveal answer
No, the search string and replacement string must be the same length. โ In standard COBOL, the search string and the replacement string in a REPLACING clause must be of the exact same length.
What is the main difference between REPLACING and CONVERTING in the INSPECT statement?
Reveal answer
REPLACING works on entire matching substrings, CONVERTING maps character-by-character. โ CONVERTING works character-by-character mapping a list of characters to another list. REPLACING looks for the exact substring to replace.
Challenges
Challenge 1 +20 XP
Use `INSPECT` to count the number of spaces in `WS-SENTENCE` and add 1 to calculate the number of words. Store the result in `WS-WORD-COUNT`. Assume words are separated by exactly one space.
cobolโ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real โ it came from compiling the solution with GnuCOBOL when this page
was built.
Test 1 โ expects "Word count: 16"
Need a hint? (โ25% XP)
Start by setting `WS-WORD-COUNT` to 1. Then, use `INSPECT ... TALLYING ... FOR ALL ' '`.
Show solution (0 XP)
IDENTIFICATION DIVISION. PROGRAM-ID. WORD-COUNT. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-SENTENCE PIC X(50) VALUE 'THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG'. 01 WS-WORD-COUNT PIC 99 VALUE ZERO. PROCEDURE DIVISION. MOVE 1 TO WS-WORD-COUNT. INSPECT WS-SENTENCE TALLYING WS-WORD-COUNT FOR ALL ' '. DISPLAY 'Word count: ' WS-WORD-COUNT. STOP RUN.
๐ Bug Hunt+20 XP
This program tries to replace 'CAT' with a 5-letter word 'MOUSE' in a sentence, but it fails to compile or behaves unexpectedly. Find and fix the bug by replacing 'CAT' with 'DOG'.
This code runs. It just does the wrong thing. Read it, find the defect,
fix it โ the tests below decide when you are right.
cobolโ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real โ it came from compiling the solution with GnuCOBOL when this page
was built.
Test 1 โ expects "THE DOG IN THE HAT "
Need a hint? (โ25% XP)
The lengths of the target string ('CAT') and the replacement string must be exactly the same.
Show solution (0 XP)
IDENTIFICATION DIVISION. PROGRAM-ID. BUGHUNT. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-TEXT PIC X(30) VALUE 'THE CAT IN THE HAT'. PROCEDURE DIVISION. INSPECT WS-TEXT REPLACING ALL 'CAT' BY 'DOG'. DISPLAY WS-TEXT. STOP RUN.