COBOL INSPECT Useful Logic to Find Spaces in String

The below logic helps you to find spaces in a string using INSPECT statement in COBOL. The sample logic tells how many spaces are present in a string.

Logic with INSPECT to find Spaces in String

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CNT1 PIC 9(2) VALUE 0.
01 WS-CNT2 PIC 9(2) VALUE 0.
01 WS-STRING PIC X(15) VALUE ‘ABCDACD””ADEAAAFF’.

PROCEDURE DIVISION.
INSPECT WS-STRING TALLYING WS-CNT1 FOR ALL CHARACTERS.
DISPLAY “WS-CNT1 : “WS-CNT1.
INSPECT WS-STRING TALLYING WS-CNT2 FOR ALL SPACES
DISPLAY “WS-CNT2 : “WS-CNT2

/* The WS_CNT2 value now is ‘2’. Since there are 2 spaces in input string*/
STOP RUN.

Related posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.