COBOL Inspect statement (Inspect statement in COBOL).
Welcome back to another exciting tutorial on “COBOL Inspect Statement”. String manipulation is an integral part of any programming language and COBOL also provides many intrinsic functions that can be used to manipulate the string, one such function is COBOL Inspect statement.
The INSPECT statement in COBOL is generally used for string manipulation in the COBOL program.
INSPECT in COBOL is primarily used for manipulating string or text. The COBOL INSPECT statement lets you count characters in a field or replace characters in a field. The INSPECT statement has four different formats that vary based on the use of the Tallying, Replacing and Converting clauses.
You can use the COBOL Inspect clause for replacing, converting or counting the characters in a given variable. A variable can be alphabetic, alphanumeric, or numeric with usage as the display. When you code CHARACTERS in the Tallying clause, it refers to all of the characters in the field that’s being inspected.
In contrast, ALL refers to all occurrences of the specified character or characters, and LEADING refers to all occurrences of the specified character or characters at the start of the field.
Inspect statement in COBOL, has following variants. Let’s discuss each variant with example.
- COBOL Inspect with Replacing Phrase.
- COBOL Inspect with Tallying Phrase.
- COBOL Inspect with Converting Phrase.
- COBOL Inspect with Tallying and Replacing Phrase.
Example 1: COBOL Inspect Statement.
.....
WORKING-STORAGE SECTION.
01 WS-DTE PIC X(10) VALUE “10-11-2020”.
PROCEDURE DIVISION.
A000-MAIN-SECTION.
INSPECT WS-DTE REPLACING ALL "-" BY "/".
Output - 10/11/2020
Example 2: COBOL Inspect with TALLYING.
.....
WORKING-STORAGE SECTION.
01 WS-NME PIC X(15) VALUE “DAVID MURPHY”.
01 WS-CNT PIC 9(03) VALUE ZEROES.
PROCEDURE DIVISION.
A000-MAIN-SECTION.
INSPECT WS-NME TALLYING WS-CNT FOR ALL SPACES.Output - 004
Example 3: COBOL Inspect with REPLACE.
.....
INSPECT WS-NME REPLACING ALL "D" BY
"d" AFTER INITIAL "D".Output - DAVId MURPHY
Example 4: COBOL Inspect with CONVERTING.
.....
INSPECT WS-NME CONVERTING
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" TO "abcdefghijklmnopqrstuvwxyz".Output - david murphy
You can use the COBOL Inspect clause with the Tallying and Replacing clauses to both count and replace characters in a field operation.
You’ll learn about each of COBOL INSPECT statement formats with examples.
Please Subscribe to Topictrick & Don’t forget to press THE BELL ICON to never miss any updates. Please visit below mention the link to stay connected with Topictrick.
► Topictrick.com
► Youtube
► Follow us on Twitter
► Facebook
► Linkedin
► Reddit
► Mainframe Blog
Thank you for your support.
Topictrick™