i want to reverse a string in cl (eg my country )... if the string is 'my country' thre output should be something like yrtnuoc ym..

This is how I'd do it:

PGM

       DCL VAR(&INPUT)  TYPE(*CHAR) LEN(10)
       DCL VAR(&OUTPUT) TYPE(*CHAR) LEN(10)
       DCL VAR(&IPOS)   TYPE(*DEC)  LEN(5 0)
       DCL VAR(&OPOS)   TYPE(*DEC)  LEN(5 0)

       CHGVAR VAR(&INPUT) VALUE('my country')

       CHGVAR VAR(&IPOS)  VALUE(10)
       CHGVAR VAR(&OPOS)  VALUE(1)


LOOP:  IF (&IPOS *GT 0) THEN(DO)
           CHGVAR VAR(%SST(&OUTPUT &OPOS 1)) +
                  VALUE(%SST(&INPUT &IPOS 1))
           CHGVAR VAR(&IPOS)  VALUE(&IPOS - 1)
           CHGVAR VAR(&OPOS)  VALUE(&OPOS + 1)
           GOTO LOOP
       ENDDO


       SNDUSRMSG MSGTYPE(*INFO) +
             MSG(&INPUT *BCAT 'backwards is' *BCAT &OUTPUT)

ENDPGM

i want the blank to be inserted in the output.i have tried by *tcat but i want a blank to be inserted in the output

That's what *TCAT does. It strips all of the blanks from the end of a string. Not only will that strip the blank from the middle of "my country", but it'd be extremely inefficient because you keep padding the string with blanks and then stripping them off over and over again in a loop. The *BCAT operator does the same thing, except that it adds one space back after it has stripped them off, so that won't help you either.

What you really want to do is only set one character at a time, rather than re-doing the whole string every time you add a letter to it. And the the way you do that is with %SST.

Check out the example I posted (above). Try it out. Look at how it works... I think it'll make more sense to you.


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.