Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Reverse a string using shift

Former Member
0 Kudos

Hi,

A quick search could not provide me with an answer whether in ABAP we can reverse a string using the SHIFT keyword alone? I am not supposed to use the function or the offset and length. Also, my data object is a string type, not a character type.

In which ever way I tried, I am breaking the code when characters repeat successive in the string.

Thanks.

11 REPLIES 11

Former Member
0 Kudos

shift circular comes to mind...

you could get the length and shift circular up to lv_length places?

In NetWeaver 7.02, I think there's a statement that does....At least, I think I saw that in Thomas Jung's presentation on the future of ABAP.

0 Kudos

The circular shift just shifts the first character tot he last and so on.

The catch is not to use the offset and length.

We could have acheived by using a combination of left right shift, but then the STRING data type will always contain all the characters however shifted.

0 Kudos

Hi ,

use function STRING_REVERSE

Pass String 'Deepak'

Language keep blank it will take default .

it will show output kapeed

Regards

Deepak.

0 Kudos

This message was moderated.

0 Kudos

Guys, thanks for posting. But I guess you overlooked my question.

I am supposed to use only the SHIFT and its variants. No offset, no function.

0 Kudos

If you are allowed to use STRLEN keyword. You can try like this.

DATA: gv_text      TYPE STRING VALUE 'Hello. How are you?',
      gv_text_temp TYPE STRING .

DATA: gv_length TYPE i,
      gv_index  TYPE i.

DATA: gt_char  TYPE TABLE OF c,
      gwa_char TYPE c.

gv_length = STRLEN( gv_text ).

gv_text_temp = gv_text.

DO gv_length TIMES.

  gv_index = gv_length - sy-index.

  SHIFT gv_text BY gv_index PLACES.

  gwa_char = gv_text.

  APPEND gwa_char TO gt_char.

  gv_text = gv_text_temp.

ENDDO.

LOOP AT gt_char INTO gwa_char.
  WRITE: gwa_char.
ENDLOOP.

0 Kudos

A combination of SHIFT LEFT and SHIFT RIGHT CIRCULAR can do the trick.


PARAMETERS in_str TYPE string.

DATA : length TYPE i,
      index TYPE i,
      rev_str LIKE in_str,
      buf_str LIKE in_str.

length = STRLEN( in_str ).
index = length - 1.
buf_str = in_str.

DO length TIMES.
  SHIFT buf_str BY index PLACES.
  CONCATENATE rev_str buf_str INTO rev_str.
  buf_str = in_str.
  SHIFT buf_str BY sy-index PLACES RIGHT CIRCULAR.
ENDDO.

WRITE / rev_str.

0 Kudos

I thought you weren't supposed to use length.

former_member214857
Contributor
0 Kudos

Hi

You can use code below as a FORM in your code

DATA:
  x_string(50) TYPE c VALUE 'ABCDE',
  x_length     TYPE i,
  x_revert(50) TYPE c,
  x_index      TYPE i.


 x_length = strlen( x_string ) + 1.


 DO x_length TIMES.
    x_index = sy-index - 1.
    CONCATENATE x_string+x_index(1) x_revert INTO x_revert.
 ENDDO.

 WRITE x_revert.

Kind regards

Former Member
0 Kudos

Use FM CALL FUNCTION 'STRING_REVERSE'


REPORT ZSTRINGREV .

Parameters: p_string(200).
Data: r_string(100).

CALL FUNCTION 'STRING_REVERSE'
  EXPORTING
    STRING          = p_string
    LANG            = 'E'
 IMPORTING
   RSTRING          = r_string
 EXCEPTIONS
   TOO_SMALL        = 1
   OTHERS           = 2
          .
IF SY-SUBRC <> 0.
 MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

*Inputted String.

Write:/ 'Inputted String', p_string.
SKIP.

*The reverse string.
Write:/ r_string.

Edited by: kk.adhvaryu on Oct 2, 2010 6:54 AM

sunojmichael1
Explorer
0 Kudos

This message was moderated.