reverse the order of a string using cobol



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

reverse the order of a string using cobol

Postby rajitha_nair01 » Sat Jan 25, 2014 5:33 pm

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?
rajitha_nair01
 
Posts: 11
Joined: Thu Aug 15, 2013 5:53 pm
Has thanked: 0 time
Been thanked: 0 time

Re: reverse the order of a string using cobol

Postby enrico-sorichetti » Sat Jan 25, 2014 7:34 pm

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
cheers
enrico
When I tell somebody to RTFM or STFW I usually have the page open in another tab/window of my browser,
so that I am sure that the information requested can be reached with a very small effort
enrico-sorichetti
Global moderator
 
Posts: 2994
Joined: Fri Apr 18, 2008 11:25 pm
Has thanked: 0 time
Been thanked: 164 times

Re: reverse the order of a string using cobol

Postby rajitha_nair01 » Sun Jan 26, 2014 2:31 am

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
rajitha_nair01
 
Posts: 11
Joined: Thu Aug 15, 2013 5:53 pm
Has thanked: 0 time
Been thanked: 0 time

Re: reverse the order of a string using cobol

Postby BillyBoyo » Sun Jan 26, 2014 3:24 am

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.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: reverse the order of a string using cobol

Postby Robert Sample » Sun Jan 26, 2014 3:31 am

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.
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: reverse the order of a string using cobol

Postby dick scherrer » Mon Jan 27, 2014 4:22 am

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.
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: reverse the order of a string using cobol

Postby tivrfoa » Mon Jan 27, 2014 5:50 am

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"
tivrfoa
 
Posts: 84
Joined: Wed Aug 22, 2012 6:35 pm
Has thanked: 60 times
Been thanked: 0 time

Re: reverse the order of a string using cobol

Postby dick scherrer » Mon Jan 27, 2014 5:55 am

Hello,

If the entire field is first reversed, embedded spaces will be handed as part of the answer.
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: reverse the order of a string using cobol

Postby tivrfoa » Mon Jan 27, 2014 6:22 pm

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"
tivrfoa
 
Posts: 84
Joined: Wed Aug 22, 2012 6:35 pm
Has thanked: 60 times
Been thanked: 0 time

Re: reverse the order of a string using cobol

Postby dick scherrer » Mon Jan 27, 2014 7:48 pm

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.
Hope this helps,
d.sch.

These users thanked the author dick scherrer for the post:
tivrfoa (Mon Jan 27, 2014 8:23 pm)
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Next

Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post