Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
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
 
How to find the length of a String in COBOL
Goto page Previous  1, 2
 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
kranthikumarb

Active User


Joined: 02 Jan 2009
Posts: 115
Location: Hyderabad

PostPosted: Thu Nov 26, 2009 4:13 pm    Post subject: Reply to: How to find the string length in COBOL
Reply with quote

I know that this thread is quite old. But the way I looked for a solution after 3 years of the post, some one might want to know a solution after n years.

This is with out using any kind of functions.

Code:

 WORKING-STORAGE SECTION.                                       
   01 VAR1   PIC X(1000) VALUE SPACES.                           
   01 VAR2   PIC X(1000) VALUE SPACES.                           
   01 WS-LEN PIC 9(04) VALUE ZEROS.                             
   01 TOTAL PIC 9(04) VALUE ZEROS.                               
   01 I PIC 9(04).                                               
   01 J PIC 9(04).                                               
  PROCEDURE DIVISION.                                           
  100-MAIN-PARA.                                                 
        DISPLAY 'ENTER STRING :'                                 
        ACCEPT VAR1                                             
        MOVE 1 TO J                                             
* TO CALCULATE THE TOTAL LENGTH CAPACITY                                 
        INSPECT VAR1 TALLYING TOTAL FOR                         
        CHARACTERS BEFORE INITIAL '\0'                           
        DISPLAY 'TOTAL LENGTH CAPACITY OF THE VARIABLE IS ' TOTAL
        MOVE TOTAL TO I                                         
* STRING REVERSING                                               
         PERFORM UNTIL I = 0                                     
            MOVE VAR1(I:1) TO VAR2(J:1)                         
            COMPUTE J = J + 1                                   
            COMPUTE I = I - 1                                   
         END-PERFORM                                             
* END OF STRING REVERSING                                       
         INSPECT VAR2 TALLYING WS-LEN FOR LEADING SPACES         
         COMPUTE WS-LEN = TOTAL - WS-LEN                         
         DISPLAY 'LENGTH IS  :' WS-LEN                           
        STOP RUN.                                               


I am happy if this helps someone icon_smile.gif
Back to top
View user's profile Send private message

Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8446
Location: Dubuque, Iowa, USA

PostPosted: Thu Nov 26, 2009 4:31 pm    Post subject:
Reply with quote

Quote:
CHARACTERS BEFORE INITIAL '\0'
You are aware, I hope, that COBOL does not support the C standard of using a hex '00' to denote the end of a string? What you are looking for here is two characters -- a backwards slash followed by a zero (hex F0) -- not a single byte of hex 00. What C defines as \0 is called in COBOL a LOW-VALUES byte. Furthermore, 3270 emulators do not terminate string data input with anything so the data accepted by the program won't have a terminator, unless the person entering the data keys one in.
Back to top
View user's profile Send private message
kranthikumarb

Active User


Joined: 02 Jan 2009
Posts: 115
Location: Hyderabad

PostPosted: Thu Nov 26, 2009 9:07 pm    Post subject:
Reply with quote

Hi,
But, In the above code the value in the variable is 1000.
How can we explain this?

I just want to know how are we getting a value of 1000 in total if it \0 doesnt say that it is the end of the allocated size for the variable.

Regards,
Kranthi Kumar
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6970
Location: porcelain throne

PostPosted: Thu Nov 26, 2009 10:40 pm    Post subject:
Reply with quote

Kranthi,

COBOL does not support the C standard of using a hex '00' to denote the end of a string

allocated size of variable is determined at compile time - not run time.

Admittedly, COBOL is not the best string manipulation language,
but if you would read the manuals, you won't have any problems.
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8446
Location: Dubuque, Iowa, USA

PostPosted: Fri Nov 27, 2009 5:17 am    Post subject:
Reply with quote

If you define the variable as PIC X(1000), the variable length is 1000. Not 1, not 10, not 100, not depending upon where a \0 occurs -- the length is 1000 bytes -- always and permanently. Unlike C and similar languages, you cannot change the length of the variable at run time -- it will always and forever be the length set at compile time.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6970
Location: porcelain throne

PostPosted: Fri Nov 27, 2009 6:18 am    Post subject:
Reply with quote

The original Enterprise Cobol Manual (several years ago),
had a comment that stipulated that, like C or JAVA,
the COBOL LENGTH function of a variable would return either
the compile time generated length,
or would reflect the length of a null (X'00') delineated string.

Subsequent versions of the manual have omitted the NULL delineated string concept

it is not in force, nor was it ever in force. The original manual was published in error
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Fri Nov 27, 2009 7:46 am    Post subject:
Reply with quote

Hello,

Quote:
you cannot change the length of the variable at run time -- it will always and forever be the length set at compile time.
While length will remain constant; You can choose how much of the variable you use.

So you might consider calculating/determining the "length to use" and when "using" the field, specify reference modification and the calculated length-to-use for those statements. . .
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
Goto page Previous  1, 2
Page 2 of 2

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts S0C7 abend while running a Cobol Program amitc23 COBOL Programming 2 Tue May 07, 2019 4:06 pm
No new posts Search string from concatenated input... srini24 JCL & VSAM 8 Sat Mar 30, 2019 9:51 pm
No new posts Can we pass cobol pointer to cobol-db... rohanthengal DB2 4 Tue Mar 19, 2019 1:04 pm
No new posts Call DB2 user defined function from C... Ziko13 DB2 5 Thu Mar 14, 2019 4:46 pm
No new posts z/VM z/VSE Get CPU-time of a COBOL Ba... Andi1982 COBOL Programming 3 Mon Mar 11, 2019 3:36 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us