String Handling

String handling statements in COBOL are used to do multiple functional operations on strings. Following are the string handling statements −

  • Inspect
  • String
  • Unstring

Inspect

Inspect verb is used to count or replace the characters in a string. String operations can be performed on alphanumeric, numeric, or alphabetic values. Inspect operations are performed from left to right. The options used for the string operations are as follows −

Tallying

Tallying option is used to count the string characters.

Syntax

Following is the syntax of Tallying option −

INSPECT input-string
TALLYING output-count FOR ALL CHARACTERS

The parameters used are −

  • input-string − The string whose characters are to be counted.
  • output-count − Data item to hold the count of characters.

Example

IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-CNT1 PIC9(2)VALUE0.01 WS-CNT2 PIC9(2)VALUE0.01 WS-STRING PICX(15)VALUE'ABCDACDADEAAAFF'.PROCEDUREDIVISION.INSPECT WS-STRING TALLYING WS-CNT1 FORCHARACTER.DISPLAY"WS-CNT1 : "WS-CNT1.INSPECT WS-STRING TALLYING WS-CNT2 FORALL'A'.DISPLAY"WS-CNT2 : "WS-CNT2
   
STOPRUN.

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

When you compile and execute the above program, it produces the following result −

WS-CNT1 : 15
WS-CNT2 : 06

Replacing

Replacing option is used to replace the string characters.

Syntax

Following is the syntax of Replacing option −

INSPECT input-string REPLACING ALL char1 BY char2.

The parameter used is −

  • input-string − The string whose characters are to be replaced from char1 to char2.

Example

IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-STRING PICX(15)VALUE'ABCDACDADEAAAFF'.PROCEDUREDIVISION.DISPLAY"OLD STRING : "WS-STRING.INSPECT WS-STRING REPLACINGALL'A'BY'X'.DISPLAY"NEW STRING : "WS-STRING.STOPRUN.

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

When you compile and execute the above program, it produces the following result −

OLD STRING : ABCDACDADEAAAFF
NEW STRING : XBCDXCDXDEXXXFF

String

String verb is used to concatenate the strings. Using STRING statement, two or more strings of characters can be combined to form a longer string. Delimited By clause is compulsory.

Syntax

Following is the syntax of String verb −

STRING ws-string1 DELIMITEDBYSPACE
   ws-string2 DELIMITEDBYSIZEINTO ws-destination-string
   WITHPOINTER ws-count
   ONOVERFLOWDISPLAY message1
   NOTONOVERFLOWDISPLAY message2
END-STRING.

Following are the details of the used parameters −

  • ws-string1 and ws-string2 : Input strings to be concatenated
  • ws-string : Output string
  • ws-count : Used to count the length of new concatenated string
  • Delimited specifies the end of string
  • Pointer and Overflow are optional

Example

IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-STRING PICA(30).01 WS-STR1 PICA(15)VALUE'Tutorialspoint'.01 WS-STR2 PICA(7)VALUE'Welcome'.01 WS-STR3 PICA(7)VALUE'To AND'.01 WS-COUNT PIC99VALUE1.PROCEDUREDIVISION.STRING WS-STR2 DELIMITEDBYSIZE
  WS-STR3 DELIMITEDBYSPACE
  WS-STR1 DELIMITEDBYSIZEINTO WS-STRING 
  WITHPOINTER WS-COUNT
  ONOVERFLOWDISPLAY'OVERFLOW!'END-STRING.DISPLAY'WS-STRING : 'WS-STRING.DISPLAY'WS-COUNT : 'WS-COUNT.STOPRUN.</pre>

JCL to execute the above COBOL program −

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

When you compile and execute the above program, it produces the following result −

WS-STRING : WelcomeToTutorialspoint       
WS-COUNT : 25

Unstring

Unstring verb is used to split one string into multiple sub-strings. Delimited By clause is compulsory.

Syntax

Following is the syntax of Unstring verb −

UNSTRING ws-string DELIMITEDBYSPACEINTO ws-str1, ws-str2
WITHPOINTER ws-count
ONOVERFLOWDISPLAYmessageNOTONOVERFLOWDISPLAYmessageEND-UNSTRING.

Example

IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-STRING PICA(30)VALUE'WELCOME TO TUTORIALSPOINT'.01 WS-STR1 PICA(7).01 WS-STR2 PICA(2).01 WS-STR3 PICA(15).01 WS-COUNT PIC99VALUE1.PROCEDUREDIVISION.UNSTRING WS-STRING DELIMITEDBYSPACEINTO WS-STR1, WS-STR2, WS-STR3
   END-UNSTRING.DISPLAY'WS-STR1 : 'WS-STR1.DISPLAY'WS-STR2 : 'WS-STR2.DISPLAY'WS-STR3 : 'WS-STR3.STOPRUN.

JCL to execute the above COBOL program −

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

When you compile and execute the above program, it produces the following result −

WS-STR1 : WELCOME
WS-STR2 : TO
WS-STR3 : TUTORIALSPOINT 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *