Exercises: Pointer and Reference Modification

Exercise 19.1: Basic Reference Modification

Given the following data item:

01  WS-DATA  PIC X(20) VALUE "ABCDEFGHIJKLMNOPQRST".

Write COBOL statements to: 1. Extract characters 5 through 8 into WS-RESULT 2. Replace characters 10 through 12 with "XYZ" 3. Extract the last 5 characters using FUNCTION LENGTH 4. Extract a substring where the start position is stored in WS-POS (PIC 99) and the length is stored in WS-LEN (PIC 99) 5. Add defensive validation before the dynamic extraction in part 4

Exercise 19.2: Delimiter Scanner

Write a program that accepts a string (up to 100 characters) and a delimiter character. Using reference modification (not UNSTRING), count how many times the delimiter appears and display the position of each occurrence.

Example: - Input: "HELLO*WORLD*FOO*BAR", Delimiter: * - Output: Found 3 occurrences at positions: 6, 12, 16

Exercise 19.3: String Reversal

Write a program that reverses a string using reference modification. Do NOT use FUNCTION REVERSE. Accept a string of up to 50 characters, determine its actual length (excluding trailing spaces), then build the reversed string one character at a time using reference modification.

Exercise 19.4: Dynamic Output Builder

Write a program that builds formatted address labels using the pointer-based output pattern. Given fields for name, street, city, state, and ZIP code (some may be blank), build a single-line address with only the non-blank fields, separated by commas and spaces.

Requirements: - Skip any blank fields entirely (no empty commas) - Track the output length using a pointer variable - Handle the case where all fields are blank - Display the final output and its length

Exercise 19.5: Variable-Format Record Parser

Write a program that processes transaction records with the following format:

Pos 1-2:   Record type ("01", "02", "03")
Pos 3-12:  Account number
Pos 13-?:  Type-dependent data

Type 01 (Deposit): Amount (12 chars) + Description (up to 50 chars, length in pos 25-26) Type 02 (Transfer): From-amount (12) + To-account (10) + Reference (8) Type 03 (Fee): Fee code (4) + Amount (12) + Reason (30)

Parse each record type correctly using reference modification and display the extracted fields.

Exercise 19.6: CSV Line Parser with Quoted Fields

Write a CSV parser that handles quoted fields containing commas. Use reference modification to scan character by character, maintaining a "within quotes" flag.

Test data:

John Smith,42,"New York, NY",Engineer
"O'Brien, Pat",38,Chicago,Manager
Jane Doe,29,"Portland, OR","Sr. Developer"

Display each field on its own line, correctly handling the quoted commas.

Exercise 19.7: EDI Element Extractor

Write a reusable utility paragraph that extracts the Nth element from an EDI segment. The segment uses * as the element separator, : as the sub-element separator, and ~ as the segment terminator.

Test with: CLM*12345*1500.00*11:B:1*Y*A~ - Extract element 1 (12345) - Extract element 3 (11:B:1) - Extract sub-element 3.2 (B) - Handle request for element 10 (beyond end of segment)

Exercise 19.8: String Padding and Alignment

Write a utility that takes a string and a target width, and produces three versions: 1. Left-aligned (padded with spaces on the right) 2. Right-aligned (padded with spaces on the left) 3. Center-aligned (padded equally on both sides)

Use reference modification exclusively — do not use JUSTIFIED RIGHT or STRING.

Exercise 19.9: MedClaim Claim Number Generator

MedClaim generates claim numbers in the format: YYDDDnnnnnC where: - YY = 2-digit year - DDD = Julian day (001-366) - nnnnn = sequential number - C = check digit

Write a program that: 1. Uses FUNCTION CURRENT-DATE and reference modification to extract year and compute Julian day 2. Maintains a sequential counter 3. Calculates a check digit (modulo-10 of the sum of all digits) 4. Builds the claim number using reference modification with a pointer 5. Validates a claim number by recalculating the check digit

Exercise 19.10: Challenge — Fixed-Width to Delimited Converter

Write a program that converts fixed-width records to pipe-delimited format. Accept a field definition table (start position and length for up to 10 fields) and a set of input records. For each record:

  1. Extract each field using reference modification
  2. Trim trailing spaces from each field
  3. Build the pipe-delimited output using the pointer pattern
  4. Handle fields that contain the pipe character (escape as "|")
  5. Write the converted records to an output file