IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Is there anyway to reverse line of string in COBOL


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
smgaru

New User


Joined: 05 Apr 2005
Posts: 5
Location: Hyderabad

PostPosted: Tue Apr 05, 2005 4:18 pm
Reply with quote

Hi,
Is there anyway to reverse line of string in COBOL (which length is not known) ??
Please let me know...

Regards
smgaru
Back to top
View user's profile Send private message
somasundaran_k

Active User


Joined: 03 Jun 2003
Posts: 134

PostPosted: Tue Apr 05, 2005 6:55 pm
Reply with quote

Yes.
Try this.
Code:

WORKING-STORAGE SECTION.                               
                                                       
01  YOUR-STRING            PIC X(4) VALUE 'ABCD'.         
01  REVER-STRING           PIC X(4).                     
                                                       
PROCEDURE DIVISION.                                   
                                                       
     MOVE FUNCTION REVERSE(YOUR-STRING) TO REVER-STRING

     DISPLAY 'REVERSED STRING:' REVER-STRING.


hth
-Som
Back to top
View user's profile Send private message
somasundaran_k

Active User


Joined: 03 Jun 2003
Posts: 134

PostPosted: Tue Apr 05, 2005 7:28 pm
Reply with quote

I'm sorry..I didn't read your post completely..
If you do not know the length of the string then you can't use the above solution.
But what do you mean by the length is unknown? There would be a maximum length for this string ,right?
So I assume you have a variable that has been defined to hold the largest string (say PIC X(80)).

Code:

WORKING-STORAGE SECTION.
 01 WS-STRING-1               PIC X(80) VALUE 'MY STRING IS HERE'.
 01 WS-REVER-STRING           PIC X(80).                         
 01 WS-TALLY                  PIC S9(04) COMP.                   
 01 WS-LEN                    PIC S9(04) COMP.                   
 
PROCEDURE DIVISION.
    INSPECT FUNCTION REVERSE(WS-STRING-1) TALLYING WS-TALLY 
           FOR LEADING SPACES                               
    COMPUTE WS-LEN = LENGTH OF WS-STRING-1 - WS-TALLY       
    MOVE FUNCTION REVERSE(WS-STRING-1(1: WS-LEN)) TO         
                  WS-REVER-STRING                           
    DISPLAY 'REVER STRING:' WS-REVER-STRING                 

               


hth
-Som
Back to top
View user's profile Send private message
smgaru

New User


Joined: 05 Apr 2005
Posts: 5
Location: Hyderabad

PostPosted: Tue Apr 05, 2005 9:11 pm
Reply with quote

Hi Som,
Thanks for showing with code also. But I have one doubt.

Already once we are reversing again why to...
MOVE FUNCTION REVERSE(WS-STRING-1(1: WS-LEN)) TO
WS-REVER-STRING

Instead of the above one will the follwoing wil work??

MOVE WS-STRING-1(WS-TALL: WS-LEN)) TO
WS-REVER-STRING

Pls let me know your clue...

Regards
smgaru
Back to top
View user's profile Send private message
somasundaran_k

Active User


Joined: 03 Jun 2003
Posts: 134

PostPosted: Tue Apr 05, 2005 9:42 pm
Reply with quote

Hi smgaru
The first Reverse function is used to find out the exact length of the string. For finding out the trailing spaces in the string (since the strings can be of variable length,as you mentioned in your initial post), I used reverse with leading spaces.
If your string is having trailing spaces
For eg:
01 WS-STRING-1 PIC X(10) VALUE 'ABCD'.
After the reverse function the string will be
$$$$$$DCBA.($ - Represents spaces). So we need to remove these spaces from the string before reversing it.

Quote:

You said
Instead of the above one will the follwoing wil work??

MOVE WS-STRING-1(WS-TALL: WS-LEN)) TO
WS-REVER-STRING

What is WS-TALL?. I guess its a typo. If you are meaning WS-TALLY
,just wondering how you are going to populate the value for WS-TALLY



hth
-Som
Back to top
View user's profile Send private message
smgaru

New User


Joined: 05 Apr 2005
Posts: 5
Location: Hyderabad

PostPosted: Thu Apr 07, 2005 8:04 pm
Reply with quote

Acually in the first reverse we are getting the string like

$$$$$$DCBA.($ - Represents spaces).

Here the string is already reversed. so again why to use REVERSE function???
Now it is enough to remove the leading spaces.

For extracting the string without leading spaces.

MOVE WS-STRING-1(WS-TALLY+1: WS-LEN)) TO
WS-REVER-STRING

In the above example WS-TALLY value is 6(spaces) and WS-LEN is 4.

Correct me if I am wrong.....

Regards
smgaru
Back to top
View user's profile Send private message
somasundaran_k

Active User


Joined: 03 Jun 2003
Posts: 134

PostPosted: Thu Apr 07, 2005 11:55 pm
Reply with quote

smgaru wrote:
Acually in the first reverse we are getting the string like

$$$$$$DCBA.($ - Represents spaces).

Here the string is already reversed. so again why to use REVERSE function???
Now it is enough to remove the leading spaces.

For extracting the string without leading spaces.

MOVE WS-STRING-1(WS-TALLY+1: WS-LEN)) TO
WS-REVER-STRING

In the above example WS-TALLY value is 6(spaces) and WS-LEN is 4.

Correct me if I am wrong.....

Regards
smgaru

The first reverse function I used is for calculating the correct length. Not for reversing the string. This is my First Reverse function.
Code:

INSPECT FUNCTION REVERSE(WS-STRING-1) TALLYING WS-TALLY 
           FOR LEADING SPACES     

Just wondering whether you tried executing this simple code before asking these doubts. icon_question.gif

hth
-Som
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts PARSE Syntax for not fix length word ... JCL & VSAM 7
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts Write line by line from two files DFSORT/ICETOOL 7
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
Search our Forums:

Back to Top