IDENTIFICATION DIVISION.
PROGRAM-ID. KATA.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-INPUT PIC A(200).
01 WS-OUT PIC A(200).
01 I PIC 9(08).
01 J PIC 9(08).
01 INP-LEN PIC 9(08).
PROCEDURE DIVISION.
DISPLAY "INPUT YOUR STRING"
ACCEPT WS-INPUT
DISPLAY "REVERSING ......."
MOVE FUNCTION LENGTH(FUNCTION TRIM(WS-INPUT TRAILING)) TO INP-LEN
DISPLAY "Just for reference : Your string is "INP-LEN " long"
MOVE 1 to I.
PERFORM VARYING J from INP-LEN by -1 UNTIL J =0
MOVE WS-INPUT(I:1) to WS-OUT(J:1)
MOVE FUNCTION TRIM(WS-OUT TRAILING) TO WS-OUT
ADD 1 to I
END-PERFORM
MOVE FUNCTION TRIM(WS-OUT TRAILING) TO WS-OUT.
DISPLAY WS-OUT
DISPLAY FUNCTION LENGTH(WS-OUT)
STOP RUN.
Run the program for input ctrl test
If you run the program you will see that the length of WS-INPUT
is :
Just for reference : Your string is 00000009 long
But if you do that for output it will say length of string is 200
Also the reversed string I get is :
tset lrtc
Which is 200 and not what I set.
Can someone explain where I went wrong and what can I do to fix it ?
(Note : I initially tried with function REVERSE
so a simple
MOVE FUNCTION REVERSE(WS-INPUT) TO WS-OUTPUT
same problem was there as well )
CodePudding user response:
FUNCTION LENGTH (source)
takes the length from source, in your case that's WS-OUT
, which is PIC A(200)
- so the answer 200 is correct.
FUNCTION TRIM (source TRAILING)
creates as every function a temporary/internal item - in this case removing trailing SPACES
from source
.
Because of your MOVE
of this temporary item with length 9 to one field which is of length 200 it gets right-padded by spaces.
Only DYNAMIC LENGTH
items get a dynamic size by MOVE
, all other items always stay with their size. [keeping "ODO" out for simplicity...]
You possibly want a nested function call: TRIM
REVERSE
/ LENGTH
:
DISPLAY FUNCTION LENGTH ( FUNCTION TRIM (WS-OUT) )
DISPLAY "-" FUNCTION REVERSE ( FUNCTION TRIM (WS-IN TRAILING) ) "-"