Reversing a String in RPG

5 pts.
Tags:
AS400 Data Definitions
RPG string operations
RPGLE Program
String manipulation
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,

Answer Wiki

Thanks. We'll let you know when a new response is added.

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.

Discuss This Question: 3  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • CharlieBrowne
    What is the rest of the code? And when you say "no running", is it creashing or just not doing anything?
    41,380 pointsBadges:
    report
  • TomLiotta
    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
    125,585 pointsBadges:
    report
  • TomLiotta
    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
    125,585 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following