INSPECT TALLYING
Part of the Tables and Text section of Coddy's COBOL Programming journey. Lesson 13 of 19.
INSPECT ... TALLYING counts characters or strings in a field and adds the count to a counter:
MOVE 0 TO N.
INSPECT WORD TALLYING N FOR ALL "a".The count is added to what N already holds, so set it to 0 first. Two INSPECTs in a row add up.
FOR ALL "an" counts a string, FOR LEADING "0" counts only the run at the start, and FOR CHARACTERS counts every character. One counter can take several ALLs:
INSPECT WORD TALLYING N
FOR ALL "a" ALL "e".BEFORE INITIAL and AFTER INITIAL limit the count to part of the field:
INSPECT REC-IN TALLYING N
FOR CHARACTERS BEFORE INITIAL ",".Padding counts too: FOR ALL SPACES on a 20-character field holding banana split counts 9.
Challenge
EasyThe program reads an account number into ACCT. Count only the zeros at its start into N with INSPECT, so 000420 prints Zeros: 03.
Example input, one line per ACCEPT: 000420.
Try it yourself
IDENTIFICATION DIVISION.
PROGRAM-ID. LEADZ.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ACCT PIC X(10).
01 N PIC 99 VALUE 0.
PROCEDURE DIVISION.
ACCEPT ACCT.
*> Count the leading zeros here
DISPLAY "Zeros: " N.
STOP RUN.