String intrinsic functions
The string functions of COBOL do the work that a paragraph of INSPECT and STRING used to do: trim a field, change its case, reverse it, replace a piece of it. They are easier to read than the paragraph, and they still count from the fixed width of the field, which is the one thing to remember about them.
TRIM, LENGTH and the case functions
TRIM removes leading and trailing spaces, LENGTH gives the declared width of a field and STORED-CHAR-LENGTH the width it really uses:
IDENTIFICATION DIVISION.
PROGRAM-ID. SF1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NAME PIC X(12) VALUE " Ada ".
01 WS-WORD PIC X(8) VALUE "Lovelace".
01 WS-R PIC X(40).
01 WS-N PIC 9(3).
PROCEDURE DIVISION.
MOVE FUNCTION TRIM(WS-NAME) TO WS-R.
DISPLAY "[" FUNCTION TRIM(WS-R) "]".
DISPLAY "stored " FUNCTION STORED-CHAR-LENGTH(WS-R).
DISPLAY "length " FUNCTION LENGTH(WS-WORD).
MOVE FUNCTION UPPER-CASE(WS-WORD) TO WS-R.
DISPLAY "upper " FUNCTION TRIM(WS-R).
MOVE FUNCTION LOWER-CASE(WS-WORD) TO WS-R.
DISPLAY "lower " FUNCTION TRIM(WS-R).
MOVE FUNCTION REVERSE(WS-WORD) TO WS-R.
DISPLAY "reverse " FUNCTION TRIM(WS-R).
MOVE FUNCTION SUBSTITUTE("JOB 001 DONE", "001", "042") TO WS-R.
DISPLAY "subst " FUNCTION TRIM(WS-R).
COMPUTE WS-N = FUNCTION ORD("A")
DISPLAY "ord " WS-N.
DISPLAY "char " FUNCTION CHAR(66).
STOP RUN.
cobc -x -free -Wall -o sf1 sf1.cob
./sf1
[Ada]
stored 000000003
length 8
upper LOVELACE
lower lovelace
reverse ecalevoL
subst JOB 042 DONE
ord 066
char A
The name field is twelve characters wide and holds Ada in the middle, so TRIM is what makes the brackets meet the letters. STORED-CHAR-LENGTH of the forty-character result field is 3 and LENGTH of the eight-character word is 8: the first asks how much of the field is in use, the second how much there is.
ORD returns the code of the first character, 66 for B and 65 for A, and CHAR the character of a code. Those two are how a COBOL program spells a character it cannot type, and how it reads one back from a file.
SUBSTITUTE and a key
SUBSTITUTE replaces every occurrence of one string with another, which is how a display name becomes a key:
IDENTIFICATION DIVISION.
PROGRAM-ID. SF2.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-INPUT PIC X(20) VALUE " mary ann smith ".
01 WS-STEP1 PIC X(20).
01 WS-STEP2 PIC X(20).
01 WS-KEY PIC X(14).
01 WS-LEN PIC 9(2).
PROCEDURE DIVISION.
MOVE FUNCTION TRIM(WS-INPUT) TO WS-STEP1.
MOVE FUNCTION UPPER-CASE(WS-STEP1) TO WS-STEP2.
COMPUTE WS-LEN = FUNCTION STORED-CHAR-LENGTH(WS-STEP2)
DISPLAY "name [" FUNCTION TRIM(WS-STEP2) "]".
DISPLAY "len " WS-LEN.
MOVE FUNCTION SUBSTITUTE(WS-STEP2(1:WS-LEN), " ", "-") TO WS-KEY.
DISPLAY "key [" FUNCTION TRIM(WS-KEY) "]".
MOVE FUNCTION REVERSE(WS-STEP2(1:WS-LEN)) TO WS-KEY.
DISPLAY "rev [" FUNCTION TRIM(WS-KEY) "]".
STOP RUN.
cobc -x -free -Wall -o sf2 sf2.cob
./sf2
name [MARY ANN SMITH]
len 14
key [MARY-ANN-SMITH]
rev [HTIMS NNA YRAM]
The reference modification (1:WS-LEN) is what makes the key correct. Without it the substitute runs over the whole twenty characters of WS-STEP2, the six spaces of the padding become hyphens, and the key of the first attempt of this program printed MARY-ANN-SMITH------: a key that is right for one name and wrong for every comparison.
The lesson is the one of reference modification in a new dress. A function sees the field, padding included, so a program that concatenates or replaces must first say how much of the field it means.
REVERSE and the trailing spaces
REVERSE reverses the whole field, so the spaces that follow a word come first in the result. A palindrome test that compares the two fields directly therefore fails for a real palindrome:
IDENTIFICATION DIVISION.
PROGRAM-ID. SF3.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-WORD PIC X(8) VALUE "level".
01 WS-REV PIC X(8).
PROCEDURE DIVISION.
MOVE FUNCTION REVERSE(WS-WORD) TO WS-REV.
DISPLAY "word [" WS-WORD "]".
DISPLAY "rev [" WS-REV "]".
IF WS-WORD = WS-REV
DISPLAY "palindrome"
ELSE
DISPLAY "not a palindrome"
END-IF.
IF FUNCTION TRIM(WS-WORD) = FUNCTION TRIM(WS-REV)
DISPLAY "palindrome after trim"
END-IF.
STOP RUN.
cobc -x -free -Wall -o sf3 sf3.cob
./sf3
word [level ]
rev [ level]
not a palindrome
palindrome after trim
The two fields hold the same letters and differ in where the spaces sit, so the plain comparison says no. TRIM on both sides is the one-line fix, and it is the same fix as in the previous section: a fixed-width field is spaces as much as it is characters.
Exercises
- Print the length of WS-NAME of the first program and the stored length of the trimmed copy. Which is bigger, and why?
- Replace every comma of a line with a semicolon using one SUBSTITUTE.
- Read the code of a space with FUNCTION ORD(" ") and the character with FUNCTION CHAR(32).
- Reverse a five-character field that holds a two-character word. Which positions do the letters land in?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does FUNCTION LENGTH return?
Answer
The declared width of the field
LENGTH of the eight-character word was 8 while STORED-CHAR-LENGTH of the result field was 3.
-
What does FUNCTION STORED-CHAR-LENGTH return?
Answer
The width the field really uses, without its trailing spaces
It answered 3 for a forty-character field that held Ada.
-
What does FUNCTION SUBSTITUTE do?
Answer
It replaces every occurrence of one string with another
JOB 001 DONE became JOB 042 DONE.
-
Why did the palindrome test fail on the plain field comparison?
Answer
The padded spaces of the field sit at the other end after the reversal
level reversed to level, and only the trimmed comparison matched.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-W PIC X(6) VALUE "abc". PROCEDURE DIVISION. DISPLAY "[" FUNCTION TRIM(W-W) "]". STOP RUN.Answer
[abc]
TRIM removes the three spaces of the padding.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-W PIC X(5) VALUE "abc". PROCEDURE DIVISION. DISPLAY FUNCTION STORED-CHAR-LENGTH(W-W). STOP RUN.Answer
3
Three characters are in use in the five-character field.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-W PIC X(4) VALUE "abcd". PROCEDURE DIVISION. DISPLAY FUNCTION REVERSE(W-W). STOP RUN.Answer
dcba
A four-character field with no padding reverses to its own letters in reverse order.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC 9(2). PROCEDURE DIVISION. COMPUTE W-A = FUNCTION ORD("B") - FUNCTION ORD("A") DISPLAY W-A. STOP RUN.Answer
01
The codes of B and A are 66 and 65, and their difference is one.
-
Why does the key of the second program need the reference modification?
Answer
The substitute would turn the padding spaces into hyphens
The first attempt printed MARY-ANN-SMITH------ for that reason.
-
Which function answers with the character of a code?
Answer
FUNCTION CHAR
CHAR(66) printed A, the reverse of what ORD does.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev