Concatenate string by its length COBOL

Dinesh picture Dinesh · Dec 20, 2012 · Viewed 51.6k times · Source

Need to concatenate 4 strings to a destination variable in cobol.

Like,

01 WS-S1 X(10) VALUE "HI ".
01 WS-S2 X(10) VALUE "HOW ".
01 WS-S3 X(10) VALUE "ARE ".
01 WS-S4 X(10) VALUE "YOU?".

to a resultant string

"HI HOW ARE YOU?"

Can anyone please help me out?

Answer

NealB picture NealB · Dec 20, 2012

Here is a working example of the STRING verb that does what you are looking for:

   IDENTIFICATION DIVISION.
   PROGRAM-ID. EXAMPLE.
   DATA DIVISION.
   WORKING-STORAGE SECTION.
   01    WS-S1 PIC X(10) VALUE 'HI '.
   01    WS-S2 PIC X(10) VALUE 'HOW '.
   01    WS-S3 PIC X(10) VALUE 'ARE '.
   01    WS-S4 PIC X(10) VALUE 'YOU?'.
   01    WS-CONCAT PIC X(43) VALUE SPACES.
   PROCEDURE DIVISION.
   MAIN-PARAGRAPH.
        STRING WS-S1 DELIMITED BY SPACE
               ' '   DELIMITED BY SIZE
               WS-S2 DELIMITED BY SPACE
               ' '   DELIMITED BY SIZE
               WS-S3 DELIMITED BY SPACE
               ' '   DELIMITED BY SIZE
               WS-S4 DELIMITED BY SPACE
          INTO WS-CONCAT
        END-STRING
        DISPLAY '>' WS-CONCAT '<'
        GOBACK
        .

Output is:

>HI HOW ARE YOU?                            <