Skip to Content
avatar image
Former Member

Reverse a string using shift

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.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • avatar image
    Former Member
    Oct 01, 2010 at 07:18 PM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 02, 2010 at 12:49 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Oct 02, 2010 at 04:54 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Oct 02, 2010 at 03:30 PM

    This message was moderated.

    Add comment
    10|10000 characters needed characters exceeded