Page 1 of 1

Reverse string in COBOL without using REVERSE function?

Posted: Thu Oct 15, 2015 2:08 pm
by alpna
Can we reverse a string in COBOL without using REVERSE function? I ask this because we have an old training mainframe computer with old COBOL compiler and that does not support REVERSE function.

Re: Reverse string in COBOL without using REVERSE function?

Posted: Thu Oct 15, 2015 3:31 pm
by William Collins
Of course you can. You'll need to define an intermediate byte, and have a loop. Save the next byte to the intermediate, MOVE the "opposite" byte (same count from the end of the data as next byte is from the beginning) to the next byte, and the saved byte to the opposite byte. Cycle the loop. Continue until the subscripts "meet" or "cross".

Re: Reverse string in COBOL without using REVERSE function?

Posted: Thu Oct 15, 2015 5:39 pm
by Robert Sample
If the compiler supports reference modification, use it. If not, you'll need to use REDEFINES to have your input and output variables accessible as an array of bytes.

Re: Reverse string in COBOL without using REVERSE function?

Posted: Mon Nov 30, 2015 2:17 pm
by alpna
For a continuous string of length 50, I have used the following logic, this seem to work:

Code: Select all

ws-counter=1 
Perform reverse-string-para 
varying ws-counter=1 from 50 by -1 
until ws-counter=0 

Reversa-para 
Move name(ws-counter:1) to namer(ws-counter2:1) 
ws-counter2=ws-counter2+1

Re: Reverse string in COBOL without using REVERSE function?

Posted: Mon Nov 30, 2015 9:00 pm
by William Collins
Well, no, it doesn't work. Use a length of 10. Sit down with a pencil and paper and work out what happens with all spaces, one character plus nine spaces, five characters (plus space), nine (plus one space) and ten characters (plus no space).