Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
Is there anyway to reverse line of string in COBOL

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
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    Post subject: Is there anyway to reverse line of string in COBOL
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Search for a string in many PDS KARTHIGADEVI CLIST & REXX 5 Fri Sep 08, 2017 2:32 pm
This topic is locked: you cannot edit posts or make replies. Extract all "IF" Statements... Adarsh Damodaran CLIST & REXX 1 Wed Sep 06, 2017 9:28 am
No new posts How to change a string containing the... chong.zhou TSO/ISPF 6 Tue Aug 29, 2017 4:54 pm
No new posts String after '.' (period) is not copy... balaji81_k SYNCSORT 6 Wed Aug 23, 2017 9:35 pm
No new posts ALPHABETIC check in COBOL vidyaa COBOL Programming 8 Thu Aug 17, 2017 7:13 pm

Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us