String handling in COBOL involves manipulating alphanumeric data, which is essential for tasks such as formatting output, parsing input, and performing various text operations. This section will cover the key concepts and operations related to string handling in COBOL.
Key Concepts
-
Alphanumeric Data Type:
- COBOL uses the
PIC X
clause to define alphanumeric data types. - Example:
01 CUSTOMER-NAME PIC X(30).
- COBOL uses the
-
String Operations:
- Concatenation: Combining two or more strings.
- Substring: Extracting a part of a string.
- Length Calculation: Determining the length of a string.
- Trimming: Removing leading or trailing spaces.
String Handling Operations
Concatenation
Concatenation in COBOL is done using the STRING
statement.
Syntax:
STRING source-string-1 DELIMITED BY size-1 source-string-2 DELIMITED BY size-2 INTO destination-string WITH POINTER pointer-name ON OVERFLOW statements END-STRING.
Example:
IDENTIFICATION DIVISION. PROGRAM-ID. StringConcatenation. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-FIRST-NAME PIC X(10) VALUE 'John'. 01 WS-LAST-NAME PIC X(10) VALUE 'Doe'. 01 WS-FULL-NAME PIC X(20). 01 WS-POINTER PIC 9(2) VALUE 1. PROCEDURE DIVISION. STRING WS-FIRST-NAME DELIMITED BY ' ' ' ' DELIMITED BY SIZE WS-LAST-NAME DELIMITED BY ' ' INTO WS-FULL-NAME WITH POINTER WS-POINTER ON OVERFLOW DISPLAY 'Overflow occurred' END-STRING. DISPLAY 'Full Name: ' WS-FULL-NAME. STOP RUN.
Substring
Extracting a substring is done using the UNSTRING
statement.
Syntax:
UNSTRING source-string DELIMITED BY delimiter INTO destination-string-1 destination-string-2 WITH POINTER pointer-name ON OVERFLOW statements END-UNSTRING.
Example:
IDENTIFICATION DIVISION. PROGRAM-ID. SubstringExample. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-FULL-NAME PIC X(20) VALUE 'John Doe'. 01 WS-FIRST-NAME PIC X(10). 01 WS-LAST-NAME PIC X(10). 01 WS-POINTER PIC 9(2) VALUE 1. PROCEDURE DIVISION. UNSTRING WS-FULL-NAME DELIMITED BY ' ' INTO WS-FIRST-NAME WS-LAST-NAME WITH POINTER WS-POINTER ON OVERFLOW DISPLAY 'Overflow occurred' END-UNSTRING. DISPLAY 'First Name: ' WS-FIRST-NAME. DISPLAY 'Last Name: ' WS-LAST-NAME. STOP RUN.
Length Calculation
The FUNCTION LENGTH
can be used to determine the length of a string.
Example:
IDENTIFICATION DIVISION. PROGRAM-ID. LengthCalculation. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NAME PIC X(20) VALUE 'John Doe'. 01 WS-NAME-LENGTH PIC 9(2). PROCEDURE DIVISION. COMPUTE WS-NAME-LENGTH = FUNCTION LENGTH(WS-NAME). DISPLAY 'Length of Name: ' WS-NAME-LENGTH. STOP RUN.
Trimming
Trimming spaces can be done using the INSPECT
statement.
Example:
IDENTIFICATION DIVISION. PROGRAM-ID. TrimmingExample. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-NAME PIC X(20) VALUE ' John Doe '. 01 WS-TRIMMED-NAME PIC X(20). PROCEDURE DIVISION. INSPECT WS-NAME REPLACING LEADING SPACES BY '' REPLACING TRAILING SPACES BY ''. MOVE WS-NAME TO WS-TRIMMED-NAME. DISPLAY 'Trimmed Name: ' WS-TRIMMED-NAME. STOP RUN.
Practical Exercises
Exercise 1: Concatenation
Write a COBOL program to concatenate three strings: "Hello", " ", and "World".
Solution:
IDENTIFICATION DIVISION. PROGRAM-ID. Exercise1. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-STRING1 PIC X(5) VALUE 'Hello'. 01 WS-STRING2 PIC X(1) VALUE ' '. 01 WS-STRING3 PIC X(5) VALUE 'World'. 01 WS-CONCATENATED-STRING PIC X(11). 01 WS-POINTER PIC 9(2) VALUE 1. PROCEDURE DIVISION. STRING WS-STRING1 DELIMITED BY SIZE WS-STRING2 DELIMITED BY SIZE WS-STRING3 DELIMITED BY SIZE INTO WS-CONCATENATED-STRING WITH POINTER WS-POINTER ON OVERFLOW DISPLAY 'Overflow occurred' END-STRING. DISPLAY 'Concatenated String: ' WS-CONCATENATED-STRING. STOP RUN.
Exercise 2: Substring
Write a COBOL program to extract the first and last names from the string "Alice Johnson".
Solution:
IDENTIFICATION DIVISION. PROGRAM-ID. Exercise2. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-FULL-NAME PIC X(20) VALUE 'Alice Johnson'. 01 WS-FIRST-NAME PIC X(10). 01 WS-LAST-NAME PIC X(10). 01 WS-POINTER PIC 9(2) VALUE 1. PROCEDURE DIVISION. UNSTRING WS-FULL-NAME DELIMITED BY ' ' INTO WS-FIRST-NAME WS-LAST-NAME WITH POINTER WS-POINTER ON OVERFLOW DISPLAY 'Overflow occurred' END-UNSTRING. DISPLAY 'First Name: ' WS-FIRST-NAME. DISPLAY 'Last Name: ' WS-LAST-NAME. STOP RUN.
Summary
In this section, we covered the basics of string handling in COBOL, including concatenation, substring extraction, length calculation, and trimming. These operations are fundamental for manipulating text data in COBOL programs. By practicing the provided exercises, you should now have a solid understanding of how to handle strings in COBOL.