Solved: function lower-case

Lower-case Function in Cobol

Cobol, an acronym for COmmon Business Oriented Language, is a high-level programming language for company applications. Despite being one of the oldest programming languages, Cobol still holds great value, particularly in the financial and administrative sector. One important string handling operation in most of these applications is converting cases. For this discussion, we will focus on implementing the lower-case function.

Understanding the Lower-case Function

The lower-case function, as the name suggests, is a function that converts all characters in a specific string into lower-case. COBOL, contrary to many popular programming languages like Python or JavaScript, does not have an in-built lower or upper-case string function. However, such a function can easily be achieved using the Cobol’s INSPECT verb, which will be the key focus here.

Tackling the Problem

Let’s understand a simple way to solve our problem. We will essentially be replacing each upper-case character with its equivalent lower-case one. This solution entails using the INSPECT verb. The approach entails making every uppercase letter replace its corresponding lowercase one systematically.

01 STRING-UPPERCASE PIC X(26) VALUE “ABCDEFGHIJKLMNOPQRSTUVWXYZ”.
01 STRING-LOWERCASE PIC X(26) VALUE “abcdefghijklmnopqrstuvwxyz”.
01 STRING-TO-CONVERT PIC X(100) VALUE “CONVERT THIS TO LOWERCASE”.
01 I PIC 99 VALUE 1.

PROCEDURE DIVISION.
A.
PERFORM B 26 TIMES.
STOP RUN.
B.
INSPECT STRING-TO-CONVERT
REPLACING ALL STRING-UPPERCASE(I:I) BY STRING-LOWERCASE(I:I).
ADD 1 TO I.

Explaining the Code

Our sample Cobol program begins its data division with four data items. STRING-UPPERCASE and STRING-LOWERCASE are defined and assigned the 26 uppercase and lowercase English alphabets respectively. STRING-TO-CONVERT holds the string to be converted into lowercase. Lastly, we have an index I initialized with 1.

Procedure division is where our logic resides. We start with performing paragraph B 26 times, where 26 corresponds to the length of our alphabets. Inside B, we use INSPECT verb on STRING-TO-CONVERT to replace each occurance of each capital letter by its lowercase counterpart. I is incremented with each iteration to move from one alphabet to another.

A Break-down on INSPECT

INSPECT is unique to COBOL and a very powerful verb meant for string handling. The verb offers various functions including counting the occurance of a specific character/string, replacing characters/strings and tallying, among others. In this context, we’ve used it for replacing each uppercase letter with its corresponding lower-case one.

Just as is with the ever-changing fashion trends, the world of coding continually evolves. Staying updated is crucial, but so is appreciating and understanding the foundations of these programming languages, such as COBOL. To draw a comparison within the fashion context, elements like color coordination and cloth fitting do differ with eras and regions, but the fundamental principles remain constant, much like in coding.

Related posts:

Leave a Comment