Page 1 of 2

reverse the order of a string using cobol

PostPosted: Sat Jan 25, 2014 5:33 pm
by rajitha_nair01
Hi all,

I want to know how can we reverse the order of a string using cobol.
for eg: the string is:

"this is a cobol string"

this should be displayed as "string cobol a is this"

can anyone help me with this?

Re: reverse the order of a string using cobol

PostPosted: Sat Jan 25, 2014 7:34 pm
by enrico-sorichetti
using the proper terminology is essential for good communication
even if the misnaming is corrected by the explanation ...
the result of reversing the string abc..xyz will be zyx...cba
what You ask for might better described as reversing the words of a sentence.

while cobol has a builtin function to reverse the characters of a string , for the question at stake
You will have to program it Yourself, using reference modification, or the unstring function
if the number of words is fixed unstring might be pretty easy,
might not be the same if the number of words is variable

Re: reverse the order of a string using cobol

PostPosted: Sun Jan 26, 2014 2:31 am
by rajitha_nair01
yes, may be i was wrong at communicating my question :oops: . The example given clearly tells what my requirement is.

If we know the number of words in a string then it is pretty easy to reverse the order . But what if the string that is being passed is not of fixed length :? .
I tried few logic and tried google it too but couldnt get any results :( .
So thought someone :geek: could help me here :?: .

THanks,
Rajitha

Re: reverse the order of a string using cobol

PostPosted: Sun Jan 26, 2014 3:24 am
by BillyBoyo
How long is your field? This gives you the maximum number of words (which would be if every word was one character, followed by one space).

Then use UNSTRING ... DELIMITED BY SPACE INTO ... a number of fields equal to the maximum possible, Set all those fields to SPACE before the UNSTRING.

Then STRING... DELIMITED BY ALL SPACE all of those fields into a new field the same size as your original. See where that takes you.

Re: reverse the order of a string using cobol

PostPosted: Sun Jan 26, 2014 3:31 am
by Robert Sample
Start at byte 1. Use reference modification to find the first space. Add 1 to the word count variable and store bytes 1 to the space in an array element. Continue until you have hit the end of the input. At this point, you can build the output variable from the array by going from the word count variable to 1, appending each array element to your output variable.

There are other ways to do this, of course -- and I have not written any code to test this.

Re: reverse the order of a string using cobol

PostPosted: Mon Jan 27, 2014 4:22 am
by dick scherrer
Hello,

Another way to get what you ask for is to reverse the entire field. Then (starting at the first position) loop across the value looking for the first non-blank. Once a non-blank is found copy each byte to your output (starting from the left) and incrementing both the input byte and the output byte. Suggest using reference modification as Robert mentioned.

Re: reverse the order of a string using cobol

PostPosted: Mon Jan 27, 2014 5:50 am
by tivrfoa
I'll test this code tomorrow, but I think it's something like this.
I used the dick scherrer idea of reversing all the string.
It may be an error with this code, depending of what you, if there are more than one space between two words.

       * -----------------------
        IDENTIFICATION DIVISION.
       * -----------------------
        PROGRAM-ID. TSTWORD.
       * -----------------------
        ENVIRONMENT DIVISION.
       * -----------------------
        WORKING-STORAGE SECTION.
       * -----------------------
        01 STR        PIC X(30).
        01 TMP-STR    PIC X(30).
        01 TMP-WORD   PIC X(30).
        01 NEW-STR    PIC X(30) VALUE SPACES.
        01 J          PIC 99.
        01 L          PIC 99.
       * -----------------------
        PROCEDURE DIVISION.
       * -----------------------
            MOVE 'this is a cobol string' TO STR
            PERFORM REVERSE-WORD
            DISPLAY NEW-STR
            STOP RUN
            .
       * -----------------------
        REVERSE-WORD SECTION.
       * -----------------------           
            INSPECT FUNCTION REVERSE(STR) TALLYING L FOR LEADING SPACES
            COMPUTE L = LENGTH OF STR - L
            MOVE FUNCTION REVERSE(STR(1:L)) TO TMP-STR
            MOVE 1 TO J
            PERFORM L TIMES   
              IF L GE 1 AND TMP-STR(L:1) EQ SPACES
                MOVE TMP-STR(J:L-1) TO TMP-WORD
                MOVE FUNCTION REVERSE(TMP-WORD) TO TMP-WORD
                STRING NEW-STR TMP-WORD DELIMITED BY SPACE INTO NEW-STR
                COMPUTE J = L + 1
              END-IF
            END-PERFORM
            .


references:
http://www.ibmmainframes.com/about4550-0.html
http://pic.dhe.ibm.com/infocenter/iadth ... 395550.htm
First result in Google: "how-to-access-single-character-from-a-string-in-cobol"

Re: reverse the order of a string using cobol

PostPosted: Mon Jan 27, 2014 5:55 am
by dick scherrer
Hello,

If the entire field is first reversed, embedded spaces will be handed as part of the answer.

Re: reverse the order of a string using cobol

PostPosted: Mon Jan 27, 2014 6:22 pm
by tivrfoa
Many bugs in my first code! :mrgreen:
Debug tool to the rescue.

Now it's working :D

Hey gurus, please tell me how the code can be improved.

      * -----------------------                                       
       IDENTIFICATION DIVISION.                                       
      * -----------------------                                       
       PROGRAM-ID. TSTWORD.                                         
      * -----------------------                                       
       DATA           DIVISION.                                       
      * -----------------------                                       
       WORKING-STORAGE SECTION.                                       
      * -----------------------                                       
       01 STR        PIC X(30).                                       
       01 TMP-STR    PIC X(30).                                       
       01 TMP-WORD   PIC X(30).                                       
       01 WS-SPACES  PIC X(30).                                       
       01 NEW-STR    PIC X(30) VALUE SPACES.                         
       01 STR-LENGTH PIC 99.                                         
       01 I          PIC 99.                                         
       01 J          PIC 99.                                         
       01 L          PIC S9(004) COMP.                               
      * -----------------------                                       
       PROCEDURE      DIVISION.                                       
      * -----------------------                                       
       MAIN           SECTION.                                       
      * -----------------------                                       
           MOVE 'THIS IS A COBOL STRING' TO STR                       
           DISPLAY STR                                               
           PERFORM REVERSE-WORD                                       
           DISPLAY NEW-STR                                           
           STOP RUN                                                   
           .                                                         
      * -----------------------                                       
       REVERSE-WORD  SECTION.                                         
      * -----------------------                                       
           INSPECT FUNCTION REVERSE(STR) TALLYING L FOR LEADING SPACES
           MOVE LENGTH OF STR TO STR-LENGTH                           
           MOVE FUNCTION REVERSE(STR(1:STR-LENGTH - L)) TO TMP-STR   
           MOVE 1 TO I J                                             
           COMPUTE L = STR-LENGTH - L + 1                           
           PERFORM L TIMES                                         
             IF I > 1 AND TMP-STR(I:1) = SPACES                     
               MOVE TMP-STR(J:I - J) TO TMP-WORD                   
               UNSTRING FUNCTION REVERSE(TMP-WORD) DELIMITED BY ALL
                   SPACES INTO WS-SPACES, TMP-WORD                 
               IF NEW-STR EQUAL SPACES                             
                 MOVE TMP-WORD TO NEW-STR                           
               ELSE                                                 
                 STRING NEW-STR(1:J - 1) TMP-WORD(1:I - J) DELIMITED
                     BY SIZE INTO NEW-STR                           
               END-IF                                               
               COMPUTE J = I + 1                                   
             END-IF                                                 
             ADD 1 TO I                                             
           END-PERFORM                                             
           .                                                       


OTHER REFERENCE:
First Google result:
"How to remove leading spaces from an alpha field"

Re: reverse the order of a string using cobol

PostPosted: Mon Jan 27, 2014 7:48 pm
by dick scherrer
Hello,

Personally, i would not use the STRING sand UNSTRING each time thru the loop.

I would reverse the "string", note the position of the first non-blank, and then move the "right-side" of the string to the output field (using reference modification as you know where to start and can calculate the length of the data to be moved..

Earlier i mentioned doing the move in a loop, but when i thought about it, decided the single move would be better for performance.