MainframeMaster

COBOL Tutorial

COBOL REVERSE Phrase - Quick Reference

Progress0 of 0 lessons

Overview

The REVERSE phrase in COBOL is used to reverse the order of characters in a string, typically using the FUNCTION REVERSE intrinsic function. It is useful for data formatting, string manipulation, and certain business logic scenarios.

Purpose and Usage

  • String Manipulation - Reverse the order of characters in a string
  • Data Formatting - Prepare data for display or processing
  • Business Logic - Implement custom logic requiring reversed data
  • Intrinsic Function - Use FUNCTION REVERSE for portability

Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
* Reverse a string using FUNCTION REVERSE MOVE FUNCTION REVERSE(name) TO reversed-name * Example 01 NAME PIC X(10) VALUE 'COBOL'. 01 REVERSED-NAME PIC X(10). PROCEDURE DIVISION. MOVE FUNCTION REVERSE(NAME) TO REVERSED-NAME DISPLAY REVERSED-NAME *> Output: 'LOBOC' * Reverse a numeric value (convert to string first) MOVE FUNCTION REVERSE(FUNCTION NUMVAL-C("12345")) TO reversed-num * Note: INSPECT REVERSED is not standard COBOL syntax

Use FUNCTION REVERSE to reverse strings. INSPECT REVERSED is not standard COBOL.

Test Your Knowledge

1. What is the primary use of the REVERSE phrase in COBOL?

  • To reverse the order of characters in a string
  • To reverse the order of records in a file
  • To reverse arithmetic operations
  • To reverse the program flow

2. Which COBOL statement or function uses REVERSE to manipulate strings?

  • INSPECT
  • FUNCTION REVERSE
  • Both
  • Neither

3. How do you reverse a string in COBOL using an intrinsic function?

  • MOVE FUNCTION REVERSE(string) TO result
  • REVERSE string INTO result
  • INSPECT string REVERSE INTO result
  • None of the above

4. Is REVERSE supported in all COBOL compilers?

  • Yes
  • No
  • Only in mainframe COBOL
  • Only in modern COBOL

5. Which of the following is a valid use of REVERSE?

  • MOVE FUNCTION REVERSE(name) TO reversed-name
  • INSPECT name REVERSED
  • REVERSE name TO reversed-name
  • All of the above

Frequently Asked Questions