To find the Length of the String



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

To find the Length of the String

Postby karthick » Tue Jan 22, 2008 1:33 pm

Hi all,

Can you please help me to get the length of the string in cobol. :cry:
karthick
 
Posts: 6
Joined: Thu Oct 11, 2007 12:16 pm
Has thanked: 0 time
Been thanked: 0 time

Re: To find the Length of the String

 

Re: To find the Length of the String

Postby arunprasad.k » Tue Jan 22, 2008 8:50 pm

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.
arunprasad.k
 
Posts: 110
Joined: Thu Dec 27, 2007 5:18 pm
Has thanked: 0 time
Been thanked: 0 time

Re: To find the Length of the String

Postby dick scherrer » Wed Jan 23, 2008 2:25 am

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


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post