Page 1 of 1

To find the Length of the String

PostPosted: Tue Jan 22, 2008 1:33 pm
by karthick
Hi all,

Can you please help me to get the length of the string in cobol. :cry:

Re: To find the Length of the String

PostPosted: Tue Jan 22, 2008 8:50 pm
by arunprasad.k
Karthick,

What do you mean by leangth? If you mean length of a string as the number of characters excluding spaces and NULL, then use the below program.

As such there is NO function in COBOL to find the length of a variable. (As per the above definition).

       WORKING-STORAGE SECTION.                                         
       77 STRING-1                 PIC X(50) VALUE 'arunprasad.k '.     
       77 INDEX-FOR-LOOP           PIC 9(02).                           
       77 LENGTH-OF-STRING-1       PIC 9(02) VALUE ZEROES.             
       PROCEDURE DIVISION.                                             
            MOVE LENGTH OF STRING-1 TO INDEX-FOR-LOOP.                 
            PERFORM UNTIL INDEX-FOR-LOOP = ZEROES                       
             IF NOT (STRING-1(INDEX-FOR-LOOP:1) = SPACES OR LOW-VALUES)
                 ADD 1 TO LENGTH-OF-STRING-1                           
             END-IF                                                     
             COMPUTE INDEX-FOR-LOOP = (INDEX-FOR-LOOP - 1) END-COMPUTE 
            END-PERFORM.                                               
            DISPLAY ' Length of STRING-1 : ' LENGTH-OF-STRING-1.       
            GOBACK.                                                     


Let me know if you have any questions.

Arun.

Re: To find the Length of the String

PostPosted: Wed Jan 23, 2008 2:25 am
by dick scherrer
Hello,

Please post your definition of "length of string" - possibly including some example(s).

Depending on what you need to know, we may have suggestions.