5 pts.
 Reversing a String in RPG
Hello Sir,
I am new in as400 programming area, In an interview I was asked How to reverse a String in rpg.
I wrote the fixed lenght string program but don't know how to write it for Variable length string.
I an declaring an array in RPG as:
 DStringR          s             15A   DIM(%len(%trim(String))  )
I am not getting any compile time error , but my program is not running.
PLease guide!  


Software/Hardware used:
V5R3 Mochasoft,
ASKED: October 20, 2011  6:39 PM
UPDATED: March 17, 2012  7:21 AM

Answer Wiki:
Here's one way to do it: <pre>D w@String S 150 Varying D w@StringRev S 150 Varying D X S 3P 0 D Y S 3P 0 D Z S 3P 0 /Free w@String = 'Now is the time'; Clear Z; Y = %Len(w@String); %Len(w@StringRev) = Y; For X = Y DownTo 1; Z += 1; %Subst(w@StringRev : Z : 1) = %Subst(w@String : X : 1); EndFor; *INLR = *On; </pre> This is a complete program. You can compile it and run it as is.
Last Wiki Answer Submitted:  May 14, 2013  8:29 pm  by  Chris Leonard   2,620 pts.
All Answer Wiki Contributors:  Chris Leonard   2,620 pts. , TomLiotta   108,330 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

What is the rest of the code?
And when you say “no running”, is it creashing or just not doing anything?

 32,935 pts.

 

I an declaring an array in RPG as:
DStringR s 15A DIM(%len(%trim(String)) )
I am not getting any compile time error…

Since %TRIM() isn’t valid in that location, it’s not clear why you wouldn’t get a compile error. Are you absolutely certain?

Tom

 108,330 pts.

 

I assume the interview is over, but here is one possiblity:

     H debug
     D string          s             15a   varying inz( 'abcdef' )

     D rev             s                   like( string )
     D pos             s             10i 0

      /free
          %len( rev ) = 0;

          for pos = %len( string )  downto 1 ;

                rev = rev + %subst( string : pos : 1 ) ;

          endfor;

          string = rev ;

          *inLR = *on ;
          dump ;
          return ;
      /end-free

Other possibilities exist.

Tom

 108,330 pts.