How to find number of Spaces in a String using INSPECT

Finding Spaces in a string using INSPECT
[The below logic helps you to find spaces in a string. The task is to find how many spaces are present in a string.]
The below logic helps you to find spaces in a string. The task is to find how many spaces are present in a 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.

Advertisements

Author: Srini

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