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.
To reverse a string, we need to call a Function Module STRING_REVERSE in ABAP program. The given program implements the same:
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.
Well, this program is self explanatory. Still, I will explain it line by line below:
- Initially, we have defined a parameter lv_data1 of type c that is character of length 10. This parameter will be used to take input from user.
- In the very next line, we have defined a variable lv_data2 of type c that is character of length 10. This variable will be used to store the reversed string.
- 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.
- That’s it, we have to now just print it as output.