String Handling in COBOL

Introduction

String handling statements in COBOL are used for performing multiple functional operations on strings.

String handling statements

String handling statements are:

1. Inspect

  • Inspect verb counts or replaces the characters in a string.
  • String operations are performed on alphanumeric, numeric, or alphabetic values.
  • These operations are performed from left to right.
Following are the options for string operations:
I. Tallying
II. Replacing

I. Tallying
Tallying operation is used for counting the string characters

Syntax:
INSPECT input-string
TALLYING output-count FOR ALL CHARACTERS

In the above syntax, the 'input string' is the string whose characters are to be counted. 'Output count' is the data item to hold the count of characters.

Example: Count string characters using INSPECT with TALLYING

IDENTIFICATION DIVISION.
PROGRAM-ID. IT.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 CNT1 PIC 9(2) VALUE 0.
   01 CNT2 PIC 9(2) VALUE 0.
   01 WS-STRING PIC X(12) VALUE 'TUTORIALRIDE'.
   
PROCEDURE DIVISION.
   INSPECT WS-STRING TALLYING CNT1 FOR ALL CHARACTERS.
   DISPLAY "CNT1 : "CNT1.
   INSPECT WS-STRING TALLYING CNT2 FOR ALL 'T'.
   DISPLAY "CNT2 : "CNT2
   
STOP RUN.


Output:
CNT1 : 12
CNT2 : 02

II. Replacing
To replace the string characters, replacing option is used.

Syntax:
INSPECT input-string REPLACING ALL char1 BY char2

Where, the 'input-string' is the string whose characters are to be replaced from char1 to char2.

Example: Program for replacing string characters

IDENTIFICATION DIVISION.
PROGRAM-ID. IR.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-STRING PIC X(15) VALUE 'AGHJAHJARTAACFA'.

PROCEDURE DIVISION.
   DISPLAY "OLD STRING BEFORE REPLACING : "WS-STRING.
   INSPECT WS-STRING REPLACING ALL 'A' BY 'T'.
   DISPLAY "NEW STRING AFTER REPALCING  : "WS-STRING.
   
STOP RUN.


Output:
OLD STRING BEFORE REPLACING : AGHJAHJARTAACFA
NEW STRING AFTER REPALCING  : TGHJTHJTRTTTCFT

2. String

  • String verb concatenates the strings. To form a longer string, two or more strings of characters are combined using STRING statement.
  • Delimited By clause is mandatory.
Syntax:
STRING ws-string1 DELIMITED BY SIZE
       ws-string2 DELIMITED BY SPACE
INTO ws-destination-string
WITH POINTER ws-count
ON OVERFLOW DISPLAY message1
NOT ON OVERFLOW DISPLAY message2
END-STRING.

In above syntax,
  • ws-string1 and ws-string2 are the input strings that are to be concatenated.
  • ws-destination-string is the output string.
  • ws-count is used for counting the length of new concatenated string.
  • The end of the string is specified by DELIMITED.
  • POINTER and OVERFLOW are optional.

Example : Program to demonstrate String

IDENTIFICATION DIVISION.
PROGRAM-ID. STR.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-STRING PIC A(40).
   01 WS-STR1 PIC A(15) VALUE 'TutorialRide'.
   01 WS-STR2 PIC A(7) VALUE 'Welcome'.
   01 WS-STR3 PIC A(16) VALUE 'To CareerRide'.
   01 WS-COUNT PIC 99 VALUE 1.

PROCEDURE DIVISION.
   STRING WS-STR2 DELIMITED BY SIZE
      WS-STR3 DELIMITED BY SPACE
      WS-STR1 DELIMITED BY SIZE
      INTO WS-STRING
      WITH POINTER WS-COUNT
      ON OVERFLOW DISPLAY 'OVERFLOW!'
   END-STRING.
DISPLAY 'WS-STRING : 'WS-STRING.
   DISPLAY 'WS-COUNT : 'WS-COUNT.

STOP RUN


Output:
WS-STRING : WelcomeToTutorialRide                   
WS-COUNT : 25

3. Unstring

  • Unstring verb is used to split a single string into multiple substrings.
  • Delimited By clause is mandatory.
Syntax:
STRING ws-string DELIMITED BY SPACE
INTO ws-string 1,  ws-string 2
WITH POINTER ws-count
ON OVERFLOW DISPLAY message
NOT ON OVERFLOW DISPLAY message
END-UNSTRING.

Example : Program to demonstrate Unstring

IDENTIFICATION DIVISION.
PROGRAM-ID. UNSTR.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-STRING PIC A(30) VALUE 'WELCOME TO TUTORIALRIDE'.
   01 STR1 PIC A(7).
   01 STR2 PIC A(2).
   01 STR3 PIC A(15).
   01 WS-COUNT PIC 99 VALUE 1.

PROCEDURE DIVISION.
   UNSTRING WS-STRING DELIMITED BY SPACE
      INTO STR1, STR2, STR3
   END-UNSTRING.
   
   DISPLAY 'STR1 IS : 'STR1.
   DISPLAY 'STR2 IS : 'STR2.
   DISPLAY 'STR3 IS : 'STR3.
STOP RUN.


Output:
STR1 IS : WELCOME
STR2 IS : TO
STR3 IS : TUTORIALRIDE