Open In App

String Handling in COBOL

The string is the data type, used to represent the sequence of characters, which ends with /0. String provides much functionality which makes our programming easy when it comes to groups of chars, words, sentences, or lines. 

String Handling:  

String handling is the process or method to handle the string functions in any programming language. In COBOL for string handling, we will use the following:



  1. Inspect Statement
  2. String Statement
  3. Unstring

Inspect Statement: Counts the number of occurrences of a given character in the string and Replaces a specific occurrence of the given character with another character. It performs from left to right. Under Inspect we have, Tallying and Replacing.

Syntax:



 INSPECT identifier-1 TALLYING                                                                                                   

  { identifier-2  For  { { ALL, LEADING } , CHARACTERS ,                              

 { identifier-3 ,literal-1 } } [ { BEFORE , AFTER }  

INITIAL { identifier-4 , literal-2 } ] }. . . . 

Example 1:




IDENTIFICATION DIVISION.
PROGRAM-ID. TALLY_EXAMPLE.
DATA DIVISION.
    WORKING-STORAGE SECTION.
        01 WS-NAME                 PIC X(20).
        01 WS-COUNTS               PIC 9(01).
PROCEDURE DIVISION.
    DISPLAY 'TALLYING EXAMPLE....'.
    MOVE "GEEKSFORGEEKS" TO WS-NAME.
    DISPLAY 'GIVEN WORD IS: ' WS-NAME.
    INSPECT WS-NAME TALLYING WS-COUNTS FOR ALL 'E'.
    DISPLAY 'COUNT OF E IN GIVEN WORDS IS: ' WS-COUNTS.
STOP RUN.

 Output:

 

Explanation: 

In this program, we are taking two input variables one is of string data type, WS-NAME which contains the input value as “GEEKSFORGEEKS” and the other is a numeric data type, WS-COUNTS for storing the count of “E” in the taken input variable. Then we are using INSPECT – TALLYING functionality to count the occurrence of “E” in the given input variable, and then display the value.

 Syntax:

INSPECT identifier-1 REPLACING ALL                                                                                            

    { identifier-2  For  { { ALL, LEADING } , CHARACTERS ,                              

   { identifier-3 ,literal-1 } } [ { BEFORE , AFTER } 

 INITIAL { identifier-4 , literal-2 } ] }……

Example 2:




IDENTIFICATION DIVISION.
PROGRAM-ID. REPLACING_EXAMPLE.
DATA DIVISION.
    WORKING-STORAGE SECTION.
        01 WS-NAME                 PIC X(10).
PROCEDURE DIVISION.
    DISPLAY 'REPLACING EXAMPLE....'.
    MOVE "BALL" TO WS-NAME.
    DISPLAY 'GIVEN WORD IS: ' WS-NAME.
    INSPECT WS-NAME REPLACING ALL 'A' BY 'E' AFTER INITIAL 'B'.
    DISPLAY 'WORD AFTER REPLACEMENT IS: ' WS-NAME.
STOP RUN.

  Output:

 

Explanation: 

In this example, we are taking one string variable, which contains the value “BALL” and then, by using INSPECT -REPLACING functionality we are trying to replace all “A” with “E” after the first occurrence of “B” in the taken input variable.

String Statements: 

Mainly used for concatenating several strings into one string. It is the “Delimited By” Phrase used in the string statements. Different phrases are there, such as Delimited By Size, Delimited By Space, and Delimited By Other-Delimiter. Here “OVERFLOW option” is also there to handle the exception when the destination string is too small to hold the concatenated string, and the “POINTER option” is there to displace the concatenated string from a particular position as desired.

Syntax:

STRING { { identifier-1, literal } . . .                                                                    

DELIMITED BY { identifier-2, literal-2, SIZE } }. . . . .

INTO identifier-3

END-STRING.

Rules for STRING Statement:

  1.  SIZE: entire variable gets transmitted.
  2.  LITERAL: transfer of data gets terminated when the specified literal is encountered.
  3.  IDENTIFIER: transfer of data gets terminated when the contents of the identifier are encountered.
  4.  SPACE: transfer of data is terminated when space is encountered.

Example 3: 




IDENTIFICATION DIVISION.
PROGRAM-ID. String_EXAMPLE.
DATA DIVISION.
    WORKING-STORAGE SECTION.
        01 WS-FIRST                PIC X(5).
        01 WS-SECOND               PIC X(10).
        01 WS-NAME                 PIC X(15).
        01 WS-VAR1                 PIC X(10).
        01 WS-VAR2                 PIC X(10).
        01 WS-NAME2                 PIC X(30).
PROCEDURE DIVISION.
    DISPLAY 'STRING STATEMENT EXAMPLES....'.
    DISPLAY '-------------------------------------------------'.
    MOVE "RUPA" TO WS-FIRST.
    MOVE "KUMARI" TO WS-SECOND.
    DISPLAY 'DELIMITED BY SIZE: '.
    STRING WS-FIRST DELIMITED BY SIZE
        '/ ' DELIMITED BY SIZE
        WS-SECOND DELIMITED BY SIZE INTO WS-NAME 
      ON OVERFLOW DISPLAY "OVERFLOW...".
        DISPLAY WS-NAME.
    DISPLAY '-------------------------------------------------'.
    MOVE "KESHAV AAA" TO WS-VAR1.
    MOVE "KUMAR BBB" TO WS-VAR2.
      
    STRING WS-VAR1 DELIMITED BY SPACE
        '/ ' DELIMITED BY SPACE
        WS-VAR2 DELIMITED BY SPACE INTO WS-NAME2 ON 
      OVERFLOW DISPLAY "OVERFLOW...".
        DISPLAY 'DELIMITED BY SPACE: '.
        DISPLAY WS-NAME2.    
STOP RUN.

Output:

 

Explanation: 

In this program, we are taking six different variables, in The first 2 variables are WS-FIRST and WS-SECOND which are part of DELIMITED BY SIZE representation. The size of the WS-FIRST variable is 5 and the size of the WS-SECOND variable is 10, So the second string will add with the first string after the 5th position. Length of WS-NAME and WS-SECOND are 5+10 which is equal to 10 and the ‘/ ‘ is occupying 2 spaces so in total we required 17 sizes of a destination string variable, but the length of WS-NAME which is the destination string is 15 which is less than the required so OVERFLOW option is performing its task and avoiding the exception to occur.

In the second part of the program, we are using DELIMITED BY SPACE, which will concatenate the two strings based on the occurrence of first space in the given variable. As represented, we had taken the variable WS-VAR1 AS “KESHAV AAA” and WS-VAR2 as “KUMAR BBB” while performing the delimited by space operation, first string will get concatenate with the second string as” KESHAV KUMAR”. 

Unstring Statement:

Syntax:

UNSTRING  identifier-1  DELIMITED BY [ ALL ]  {                                                                  

identifier-2 , literal-1 }

[ OR [ALL ]  { identifier-3 , literal-2  } ] . . . 

 INTO identifier-4 . . . 

 [END-UNSTRING].

 Example 4:




IDENTIFICATION DIVISION.
PROGRAM-ID. UNString_EXAMPLE.
DATA DIVISION.
    WORKING-STORAGE SECTION.
        01 WS-FIRST             PIC X(5).
        01 WS-MID               PIC X(10).
        01 WS-LAST              PIC X(5).
        01 WS-NAME              PIC X(25).
PROCEDURE DIVISION.
    DISPLAY 'UNSTRING STATEMENT EXAMPLES....'.
    DISPLAY '-------------------------------------------------'.
    MOVE "RUPA_KUMARI_JHA" TO WS-NAME.
    UNSTRING WS-NAME DELIMITED BY '_'
        INTO WS-FIRST, WS-MID , WS-LAST.
        DISPLAY WS-FIRST.
        DISPLAY WS-MID.
        DISPLAY WS-LAST.
STOP RUN.

Output:

 

Explanation:

In this program we are taking a variable named WS-NAME which contains the value “RUPA_KUMARI_JHA”, we are splitting the value of the variable into 3 different variables named WS-FIRST, WS-MID, WS-LAST based on the occurrence of  ‘_’  in the taken input variable.


Article Tags :