# Prog to calculate the length of the string within a variable

Author Message
sampathkmn
Warnings : 1

New User

Joined: 12 Dec 2005
Posts: 31
Location: bangalore

 Posted: Tue Jan 31, 2006 3:08 pm hi everybody, i have a doubt: how to code in a cobol program to calculate the length of the string within a variable? Eg: 01 A PIC X(80). and i am passing 40 length string to A(say i am passing any random value) and i wish to find out the exact length of string stored in variable A.
i413678
Currently Banned

Active User

Joined: 19 Feb 2005
Posts: 112
Location: chennai

 Posted: Tue Jan 31, 2006 3:26 pm Hi, I dont think so there is direct function to find the length of the string.... for this you need to reverse the string ans inspect the reversed string till you find the character...gives the total number of training spaces. subtract this spaces count from the full length you will get the length of string given by you..... correct me if i am wrong..... pavan
elonics

New User

Joined: 05 Jul 2005
Posts: 49
Location: India

Posted: Tue Jan 31, 2006 3:55 pm

Hai friend,
We have a function which tells the length of a string that has been declared. i.e

If WS-STR is declared as

 Quote: 01 WS-STR PIC X(10).

Then by using the below function we can find the length as 10

 Quote: DISPLAY LENGTH OF WS-STR

But you want to know how many non-blank characters a string has then use the following code according to ur requirement

 Quote: 01 WS-LEN PIC S9(04) COMP. MOVE 'UDAY' TO WS-STR PERFORM VARYING I FROM 1 BY 1 UNTIL WS-STR(I:1) = ' ' DISPLAY WS-STR(I:1) IF WS-STR(I:1) NOT EQUAL SPACE ADD 1 TO WS-LEN END-IF END-PERFORM DISPLAY 'WS-LEN - ' WS-LEN

This will fetch you WS-LEN as 4. Hope this meets ur requirement.

Thanks
Uday.

---------------------------------------------------------------------
'I may be the best or atleast better than many but never worst'
i413678
Currently Banned

Active User

Joined: 19 Feb 2005
Posts: 112
Location: chennai

 Posted: Tue Jan 31, 2006 4:24 pm Hi elonics thanks for your information I learned one new thing....... pavan
dipanshu

New User

Joined: 16 Jan 2006
Posts: 53
Location: pune

 Posted: Wed Feb 01, 2006 10:43 am Hi, As I understood, above coding is only meant for single spaces, what if there are multiple spaces? For example:- 'Abd sher khahn' How to calculate the length for this?
elonics

New User

Joined: 05 Jul 2005
Posts: 49
Location: India

Posted: Wed Feb 01, 2006 4:09 pm

Here is the answer to the above question. It finds how may non blank characters in a string .

 Quote: 01 WS-STR PIC X(30). 01 WS-LEN-1 PIC S9(04) COMP. 01 WS-LEN-2 PIC S9(04) COMP.

 Quote: MOVE 'UDAY KIRAN M' TO WS-STR MOVE LENGTH OF WS-STR TO WS-LEN-2 PERFORM VARYING I FROM 1 BY 1 UNTIL I > WS-LEN-2 DISPLAY WS-STR(I:1) IF WS-STR(I:1) EQUAL SPACE ADD 1 TO WS-LEN-1 ELSE ADD 1 TO WS-LEN-1 END-IF END-PERFORM SUBTRACT WS-LEN-1 FROM WS-LEN-2 DISPLAY 'WS-LEN - ' WS-LEN-2.

This will give you WS-LEN-2 as 10

Udaykiran.M
-----------------------------------------------------------------------
"I am the best or atleast better than many but never worst"
pa1chandak
Currently Banned

New User

Joined: 31 Jan 2006
Posts: 55

 Posted: Wed Feb 01, 2006 5:36 pm DISPLAY LENGTH OF WS-STR WILL GIVE YOU THE LENGTH OF FIELD DO SEND ME THE REPLY PAWAN C. 09822546416 pa1chandak@yahoo.com
elonics

New User

Joined: 05 Jul 2005
Posts: 49
Location: India

 Posted: Thu Feb 02, 2006 9:41 am Hai pa1chandak, "DISPLAY LENGTH OF WS-STR" will give you only length that it is declared with but not that what it actually contains. El'onics --------------------------------------------------------------------- "I am the best or atleast better than many but never worst"
sudheer_kumar

New User

Joined: 27 Dec 2005
Posts: 16

 Posted: Thu Feb 02, 2006 3:11 pm Hai pa1chandak, I analyzed the code. I think your logic is not correct. For reason see below. IF WS-STR(I:1) EQUAL SPACE ADD 1 TO WS-LEN-1 ---- > Same ELSE ADD 1 TO WS-LEN-1 ---- > Same END-IF I think for the above reason we can remove the If condition and just we use PERFORM VARYING I FROM 1 BY 1 UNTIL I > WS-LEN-2 DISPLAY WS-STR(I:1) ADD 1 TO WS-LEN-1 END-PERFORM I ran your code using IBM VISUAL AGE COBOL . The output is a infinite loop.
elonics

New User

Joined: 05 Jul 2005
Posts: 49
Location: India

 Posted: Thu Feb 02, 2006 3:51 pm Hai Sudheer, I have done one mistake while posting.Sorry for that.The portion which is in else part is commented out in my program. But my logic is exactly correct. Usethis MOVE 'UDAY KIRAN M' TO WS-STR MOVE LENGTH OF WS-STR TO WS-LEN-2 PERFORM VARYING I FROM 1 BY 1 UNTIL I > WS-LEN-2 IF WS-STR(I:1) EQUAL SPACE ADD 1 TO WS-LEN-1 END-IF END-PERFORM SUBTRACT WS-LEN-1 FROM WS-LEN-2 DISPLAY 'WS-LEN - ' WS-LEN-2. It will give you ws-len as 10 as i said earlier El'onics.
Aji

New User

Joined: 03 Feb 2006
Posts: 53
Location: Mumbai

 Posted: Fri Feb 03, 2006 3:45 pm hi I will give the logic to find the exact length of a variable. Initialize ws-cnt. inspect A converting " " to "^". inspect A tallying ws-cnt for all "^". compute ws-cnt = length of A - ws-cnt. inspect A converting "^" to " ". ws-cnt will give u the exact length of A. REGARDS Aji Cherian
DavidatK

Active Member

Joined: 22 Nov 2005
Posts: 700
Location: Troy, Michigan USA

Posted: Sat Feb 04, 2006 3:04 am

Sampathkmn,

 Quote: hi everybody, i have a doubt: how to code in a cobol program to calculate the length of the string within a variable? Eg: 01 A PIC X(80). and i am passing 40 length string to A(say i am passing any random value) and i wish to find out the exact length of string stored in variable A.

If I move ?Prog to calculate the length of the string within a variable? to A, I count the string as 60, including imbedded spaces. If you count the number of non spaces (not what was asked for) you get 50.

This code will give you the string-len.

 Code: 05  STRING-LEN         PIC 9(9) COMP-3.     05  A                  PIC X(80).     MOVE ?LENGTH OF STING WITHIN A VARIABLE?                            TO A.     PERFORM       VARYING STRING-LEN FROM 80 BY -1       UNTIL A(STRING-LEN:1) NOT = ? ?       OR STRING-LEN > 1     END-PERFORM.

At this point, the variable string-len will have a value of 33, the length of the string ?LENGTH OF STING WITHIN A VARIABLE?.

Sampathkmn, Please respond with exactly what you were looking for.

Dave
 View Bookmarks All times are GMT + 6 Hours

 Topic Forum Replies Similar Topics Cobol prog to put an indicator on the... JCL & VSAM 1 Creating CSV file from Variable recs ... DFSORT/ICETOOL 11 Evaluate variable to execute a jcl step JCL & VSAM 3 Inserting variable length into output... JCL & VSAM 2 Need to find a specific STRING COBOL Programming 11
Search our Forums:

 IBMMainframes.com is not an official and/or affiliated with IBM® in anyway Board Rules | FAQ | Downloads | Wiki | SiteMap | Contact Us