I often use web-based environments, to test the syntax of particular statements in programing languages. As a fun challenge, I decided to see if I could find a web-based environment to test COBOL code. I was expecting this to be an impossible quest because; “why would you?” 😊
After trying a couple of sites, I landed on tutorials point’s web site:
Tutorialspoint online COBOL compiler
Using this website, I was able to write a simple COBOL program to reverse a string. Some of the key concepts used are
a. PERFORM VARYING
b. One dimensional table
c. Reference modification, aka COBOL speak for substring
Below is a screen shot of the web interface. The source code is displayed in the left panel. When you press the execute button, the right panel is a terminal that accepts input and displays output.
Occasionally the web interface displays its true unix/php identity with a message such as:
This can be remediated by putting the string in quotes as shown below:
The complete source code in text is below:
IDENTIFICATION DIVISION. PROGRAM-ID. REVERSESTRING. DATA DIVISION. WORKING-STORAGE SECTION. 01 GENERAL-VARIABLES. 05 WS-ORIGINAL-TXT PIC X(100). 05 WS-REVERSE-TXT-TABLE. 10 WS-REVERSE-TXT-ITEM PIC X(1) OCCURS 100 TIMES. 05 WS-I PIC S9(3) USAGE IS COMP-3 VALUE 0. 05 WS-J PIC S9(3) USAGE IS COMP-3 VALUE 0. 05 WS-TEMP PIC X(01). 05 WS-ORIG-TXT-LEN PIC S9(3) USAGE IS COMP-3. PROCEDURE DIVISION. DISPLAY "Enter a string to reverse: " ACCEPT WS-ORIGINAL-TXT PERFORM VARYING WS-I FROM 100 BY -1 UNTIL ( WS-I = 1 OR WS-TEMP <> ' ') MOVE WS-ORIGINAL-TXT(WS-I:1) TO WS-TEMP END-PERFORM COMPUTE WS-ORIG-TXT-LEN = WS-I + 1 DISPLAY "You entered : " WS-ORIGINAL-TXT(1:WS-ORIG-TXT-LEN) PERFORM VARYING WS-I FROM WS-ORIG-TXT-LEN BY -1 UNTIL (WS-I = 0) COMPUTE WS-J = WS-J + 1 MOVE WS-ORIGINAL-TXT(WS-I:1) TO WS-REVERSE-TXT-ITEM(WS-J) END-PERFORM DISPLAY "Reverse string : " WS-REVERSE-TXT-TABLE STOP RUN.