Program to Reverse A String in ABAP

Sometimes, there is a need to know the reverse of a string provided by user. In that case we program to reverse a string in ABAP. It is not a question that is merely asked in an interview but an important keyword that is used in day to day programming. This even helps us to find if a string is palindrome or not.

Introduction

To reverse a string, we need to call a Function Module STRING_REVERSE in ABAP program. The given program implements the same:

Program to Reverse A String
Program to Reverse A String- Image Illustration

ABAP Program

PARAMETERS: lv_data1(10) type c.
Data : lv_data2(10) type c.
CALL FUNCTION 'STRING_REVERSE'
  EXPORTING
    string          =  lv_data1
    lang            = sy-langu
 IMPORTING
   RSTRING         = lv_data2
* EXCEPTIONS
*   TOO_SMALL       = 1
*   OTHERS          = 2
          .
IF sy-subrc EQ 0.
Write: lv_data2.
ENDIF.

Explanation

Well, this program is self explanatory. Still, I will explain it line by line below:

  1. Initially, we have defined a parameter lv_data1 of type that is character of length 10. This parameter will be used to take input from user.
  2. In the very next line, we have defined a variable lv_data2 of type that is character of length 10. This variable will be used to store the reversed string.
  3. Now, we will call a Function Module ‘STRING_REVERSE’ exporting lv_data1 i.e. our string and importing the RSTRING in our variable lv_data2.
  4. That’s it, we have to now just print it as output.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.