Day 19: COBOL String Handling – Manipulating and Formatting Text Data Effectively

 


1. Introduction: Why String Handling Is Essential in COBOL

While COBOL is known for its strength in numerical and business data processing, string handling is equally important. Real-world business applications deal with vast amounts of textual data — names, addresses, product descriptions, invoice details, etc.

COBOL provides a robust set of string manipulation verbs to make text processing efficient and readable. These verbs help you:

  • Combine multiple text fields into one (e.g., name + surname)

  • Extract specific parts of a string (e.g., area code from a phone number)

  • Validate or clean data before storing it in files or reports

Some of COBOL’s most commonly used string operations include:

  • STRING – Combines multiple fields.

  • UNSTRING – Splits one string into multiple fields.

  • INSPECT – Analyzes or replaces characters in a string.

Understanding these will help you handle almost any textual transformation required in a COBOL application.


2. Example: Using STRING, UNSTRING, and INSPECT in COBOL

Here’s a hands-on example demonstrating all three key string operations.

Program: STRHANDLE.cbl

IDENTIFICATION DIVISION. PROGRAM-ID. STRHANDLE. DATA DIVISION. WORKING-STORAGE SECTION. 01 FIRST-NAME PIC A(10) VALUE "ALICE". 01 LAST-NAME PIC A(10) VALUE "BROWN". 01 FULL-NAME PIC A(25). 01 ADDRESS PIC X(40) VALUE "123, MAIN STREET, NEW YORK". 01 STREET PIC X(20). 01 CITY PIC X(20). 01 SAMPLE-TEXT PIC X(30) VALUE "HELLO COBOL WORLD". 01 COUNT-RESULT PIC 9(3). PROCEDURE DIVISION. DISPLAY "----- STRING OPERATION -----" STRING FIRST-NAME DELIMITED BY SPACE " " DELIMITED BY SIZE LAST-NAME DELIMITED BY SPACE INTO FULL-NAME END-STRING DISPLAY "FULL NAME: " FULL-NAME DISPLAY "----- UNSTRING OPERATION -----" UNSTRING ADDRESS DELIMITED BY "," INTO STREET, CITY END-UNSTRING DISPLAY "STREET: " STREET DISPLAY "CITY: " CITY DISPLAY "----- INSPECT OPERATION -----" INSPECT SAMPLE-TEXT TALLYING COUNT-RESULT FOR ALL "O" DISPLAY "NUMBER OF 'O's: " COUNT-RESULT INSPECT SAMPLE-TEXT REPLACING ALL "O" BY "0" DISPLAY "MODIFIED TEXT: " SAMPLE-TEXT STOP RUN.

Explanation:

  • STRING – Combines FIRST-NAME and LAST-NAME into FULL-NAME.

  • UNSTRING – Breaks ADDRESS into STREET and CITY using commas as delimiters.

  • INSPECT

    • TALLYING counts occurrences of a character.

    • REPLACING substitutes one character for another.

These operations allow COBOL programs to process and transform textual data efficiently — essential for generating reports, cleaning data, or preparing structured outputs.


3. Real-World Applications and Best Practices

Practical Scenarios:

  • ๐Ÿงพ Report Generation: Format customer names or product details neatly.

  • ๐Ÿ“ง Data Validation: Verify email or phone number patterns.

  • ๐Ÿฆ Banking Systems: Clean and normalize text data before transactions.

  • ๐Ÿ“ฆ Inventory Systems: Combine product codes and categories for identifiers.

Best Practices:

  • Always use DELIMITED BY carefully to avoid truncation.

  • Use INSPECT for counting, replacing, or verifying text integrity.

  • Allocate enough space (PIC X(n)) in receiving fields to prevent overflow.

  • When using UNSTRING, define the target fields clearly to match expected delimiters.

With these techniques, COBOL becomes a powerful tool not just for number crunching but also for structured text processing.


๐Ÿง  Key Takeaways

  • STRING joins multiple text fields into one.

  • UNSTRING splits a text field into multiple parts.

  • INSPECT analyzes or modifies characters in strings.

  • COBOL’s string verbs simplify data cleaning and report formatting.

  • Proper text handling enhances readability and accuracy in business outputs.


๐Ÿ’ป Practice Task

  1. Create a program CUSTFORMAT.cbl that:

    • Accepts Customer Name, City, and Country.

    • Combines them into a single formatted string:
      "Customer: [Name], Location: [City] - [Country]"

    • Then splits it back using UNSTRING and displays each component.

  2. Use INSPECT to count how many times the letter “A” appears in the name.

  3. Bonus: Replace all lowercase letters in the city name with uppercase.



"This Content Sponsored by SBO Digital Marketing.

Mobile-Based Part-Time Job Opportunity by SBO!

Earn money online by doing simple content publishing and sharing tasks. Here's how:

  • Job Type: Mobile-based part-time work
  • Work Involves:
    • Content publishing
    • Content sharing on social media
  • Time Required: As little as 1 hour a day
  • Earnings: ₹300 or more daily
  • Requirements:
    • Active Facebook and Instagram account
    • Basic knowledge of using mobile and social media

For more details:

WhatsApp your Name and Qualification to 9994104160

a.Online Part Time Jobs from Home

b.Work from Home Jobs Without Investment

c.Freelance Jobs Online for Students

d.Mobile Based Online Jobs

e.Daily Payment Online Jobs

Keyword & Tag: #OnlinePartTimeJob #WorkFromHome #EarnMoneyOnline #PartTimeJob #jobs #jobalerts #withoutinvestmentjob"

Comments