Manipulating null-terminated strings

You can construct and manipulate null-terminated strings (for example, strings that are passed to or from a C program) by various mechanisms.

About this task

For example, you can:

  • Use null-terminated literal constants (Z". . . ").
  • Use an INSPECT statement to count the number of characters in a null-terminated string:
    
    MOVE 0 TO char-count
    INSPECT source-field TALLYING char-count
                         FOR CHARACTERS
                         BEFORE X"00"
    
  • Use an UNSTRING statement to move characters in a null-terminated string to a target field, and get the character count:
    
    WORKING-STORAGE SECTION.
    01  source-field          PIC X(1001).
    01  char-count     COMP-5 PIC 9(4).
    01  target-area.
        02 individual-char OCCURS 1 TO 1000 TIMES DEPENDING ON char-count
                              PIC X.
    . . .
    PROCEDURE DIVISION.
        UNSTRING source-field DELIMITED BY X"00"
                              INTO target-area
                              COUNT IN char-count
          ON OVERFLOW
            DISPLAY "source not null terminated or target too short"
        END-UNSTRING
    
  • Use a SEARCH statement to locate trailing null or space characters. Define the string being examined as a table of single characters.
  • Check each character in a field in a loop (PERFORM). You can examine each character in a field by using a reference modifier such as source-field (I:1).

Example: null-terminated strings

Related references  
Alphanumeric literals (Enterprise COBOL for z/OS® Language Reference)