Analyze the COBOL INSPECT verb—how to count occurrences of a character in a string.
- User
Analyze the COBOL
INSPECTverb—how to count occurrences of a character in a string. Assistant
Analyze the COBOL INSPECT verb—how to count occurrences of a character in a string.
1. Understand the Goal: The user wants to know how to use the COBOL INSPECT verb specifically to count occurrences of a character within a string.
Identify the Core Concept: The relevant clause within the INSPECT verb for counting is TALLYING.
Determine the Syntax:
INSPECT identifier-1 TALLYING identifier-2 FOR ...CHARACTERS, ALL literal, LEADING literal.Formulate the Explanation Structure:
INSPECT TALLYING does.TALLYING, ALL, LEADING, and CHARACTERS.INSPECT statement (crucial point!).ALL vs LEADING distinction.Drafting the Content (Iterative refinement):
Drafting Syntax:
INSPECT ws-source-string TALLYING ws-counter FOR ALL 'X'.
Refining the Initialization Point: I need to emphasize that INSPECT adds to the counter; it doesn't set it. So MOVE 0 TO ws-counter is mandatory.
Drafting Example 1 (Basic): Source: "HELLO WORLD" Count: "L" Result: 3
Drafting Example 2 (Multiple characters):
Can you count multiple things at once? Yes. INSPECT ... TALLYING A FOR ALL 'X', B FOR ALL 'Y'. This is a good advanced tip.
Drafting Example 3 (Replacing vs Counting): The prompt specifically asks for counting, so I shouldn't confuse the user with REPLACING unless it's a brief mention at the end. I will stick to TALLYING.
Finalizing the Output Structure (similar to the generated response):
INSPECT ... TALLYING statement.ALL: Count every occurrence.LEADING: Count only until the first mismatch.CHARACTERS: Count total length or length before/after delimiters.MOVE 0 TO.Review against COBOL standards: The syntax INSPECT source TALLYING counter FOR ALL 'x' is standard ANSI-74/85 COBOL.
Self-Correction during drafting: I initially thought about explaining INSPECT ... CONVERTING, but that's for swapping characters. Stick strictly to counting to answer the prompt accurately. I should also mention that the counter must be a numeric integer field (PIC 9).
Final Polish: Ensure the tone is technical but accessible. Use formatting (bolding, code blocks) to make it readable. (Proceed to generate output).
In COBOL, the INSPECT verb is used to scan a string (an alphanumeric data item). To count occurrences of a specific character or substring, you use the TALLYING phrase.
Here is a comprehensive guide on how to use it.
INSPECT identifier-1 TALLYING identifier-2 FOR
{ ALL literal-1 }
...
identifier-1: The source string you are searching through.identifier-2: The numeric counter (must be PIC 9) where the count is stored.ALL: Tells COBOL to count every instance of the target.literal-1: The specific character (or string) you are counting.The INSPECT verb adds to the counter; it does not set the counter. If your counter variable (identifier-2) contains "5" before the INSPECT statement runs, and there are 2 matches, the result will be 7.
You must initialize the counter to zero immediately before the INSPECT.
Let's count how many times the letter "A" appears in a string.
IDENTIFICATION DIVISION.
PROGRAM-ID. COUNT-CHARS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STRING PIC X(30) VALUE "ALABAMA IS A STATE".
01 WS-COUNTER PIC 9(02) VALUE 0.
01 WS-TARGET PIC X(01) VALUE "A".
PROCEDURE DIVISION.
DISPLAY "Original String: " WS-STRING
*> Step 1: Initialize counter (Crucial!)
MOVE 0 TO WS-COUNTER.
*> Step 2: Perform Inspection
INSPECT WS-STRING TALLYING WS-COUNTER FOR ALL "A".
DISPLAY "Count of A: " WS-COUNTER.
STOP RUN.
Output:
Original String: ALABAMA IS A STATE
Count of A: 05
It is very common to use INSPECT to find the length of a string by counting the trailing spaces, or simply to count spaces within text.
MOVE 0 TO WS-COUNTER.
INSPECT WS-STRING TALLYING WS-COUNTER FOR ALL SPACES.
COBOL allows you to chain conditions in a single statement. You can count different characters simultaneously into different counters.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TEXT PIC X(20) VALUE "COBOL IS OLD SCHOOL".
01 WS-COUNT-COMMA PIC 9(02).
01 WS-COUNT-O PIC 9(02).
01 WS-COUNT-SPACE PIC 9(02).
PROCEDURE DIVISION.
MOVE 0 TO WS-COUNT-COMMA
WS-COUNT-O
WS-COUNT-SPACE.
INSPECT WS-TEXT
TALLYING
WS-COUNT-O FOR ALL "O"
WS-COUNT-SPACE FOR ALL SPACES.
DISPLAY "O count: " WS-COUNT-O.
DISPLAY "Space count: " WS-COUNT-SPACE.
Note: If a character is counted in the first condition, it is "consumed" and usually not counted in the subsequent condition within the same INSPECT statement if they overlap (though ALL checks against non-overlapping matches, usually INSPECT processes left-to-right).
LEADINGIf you only want to count characters at the very beginning of the string (for example, counting leading zeros), use LEADING instead of ALL.
01 WS-NUMBER PIC X(10) VALUE "0000123456".
01 WS-ZERO-CTR PIC 9(02).
MOVE 0 TO WS-ZERO-CTR.
INSPECT WS-NUMBER TALLYING WS-ZERO-CTR FOR LEADING "0".
Result: WS-ZERO-CTR will be 4. As soon as it hits the '1', the counting stops.
PIC 9) for the result.MOVE 0 to that field before inspecting.INSPECT string TALLYING counter FOR ALL "char".