Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Propercase/Titlecase or Upcasing in COBOL?

Status
Not open for further replies.

Fab64

Programmer
Oct 31, 2006
3
US
Hi all,

I've just returned to COBOL programming after an absence of many, many years. The shop in which I work is currently running COBOL74, on a Unisys mainframe. Does anyone have any suggestions on how to perform Titlecasing or Uppercasing on a string? By "Titlecase" I mean converting "first name" to "First Name". By "Uppercasing" I mean converting "first name" to "FIRST NAME". I don't know if it's possible to do this efficiently in COBOL or not, but I didn't want to re-invent the wheel if someone already has something. Thanks in advance.

F.A.B.
 
Since your compiler does not support intrinsic functions, you're left with:

UPPER-CASE: INSPECT ... REPLACING ...
TITLE-CASE: Roll your own.

Regards.

Glenn
 
If your Cobol74 compiler admits it you may consider the TRANSFORM statement:
TRANSFORM yourVariable
FROM "abcdefghijklmnopqrstuvwxyz"
TO "ABCDEFGHIJKLMNOPQRSTUVWXYZ".

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Then there are those pesky names with spaces in them and the Mc Houlihan type of names or one like Di Angelo.

If you do not like my post feel free to point out your opinion or my errors.
 
Uppercasing is trivial, though tedious, using INSPECT ... REPLACING.

If COBOL74 permits multi-character operands in its INSPECT syntax, then you might consider something like:
Code:
INSPECT field-name REPLACING ALL SPACE BY "|".
INSPECT field-name REPLACING 
        ALL "|a" BY " A"
        ALL "|b" BY " B"
        ALL "|c" BY " C"
     ...
        ALL "|y" BY " Y"
        ALL "|z" BY " Z".
INSPECT field-name REPLACING ALL "|" by SPACE.
[small]n.b. my ability to remember 1974 syntax is limited.[/small]

Alternatively, since Unisys has a binary data type (IIRC) you could do some character-by-character arithmetic. Not recommended.

Tom Morrison
 
@k5tm

This doesn't take care of the first word in the variable...

HTH
TonHu
 
I do title casing extensivly in my applications. Would you be interested in a copy of my code?
 
Thanks to everyone for your great suggestions so far.

And, webrabbit, yes I would like to see your code - thanks!

FAB
 
Code:
 Identification Division.
 Program-ID.    TO-MIXED.
 Author.        James C. Fairfield

 Data Division.
 Working-Storage Section.
 01.
     05  MX                      Pic 9(02) Comp-5.
     05  MP                      Pic 9(02) Comp-5.
     05  MIXED-AREA.
         10  MA                  Pic X(01) occurs 40 times.
     05  redefines MIXED-AREA.
         10  MN                  Pic X(01) occurs 40 times Comp-X.
     05  LOWER-AREA              Pic X(40).
     05  UPPER-AREA              Pic X(40).

 Linkage Section.
 77  PARM-1                      Pic X(40).
 77  PARM-2                      Pic 9(02).

 Procedure Division using PARM-1 PARM-2.
     *> Notes: CBL_TOLOWER and CBL_TOUPPER are Micro Focus COBOL routines.
     *>        If you are not using Micro Focus COBOL, you will have to
     *>        substitute equivalent code.
     *>        MN is the binary redefinition of the characters in MIXED-AREA.
     *>        Arithmetic on this field assumes ascii.  If you are using
     *>        another alphabet (such as EBCDIC), you must either figure out
     *>        the equivalent conversion, or replace the arithmetic
     *>        manipulation by a string manipulation.
     Move PARM-2               to MP
     Move PARM-1(1:MP)         to MIXED-AREA
     Move MIXED-AREA           to LOWER-AREA UPPER-AREA
     Call 'CBL_TOLOWER'     using LOWER-AREA by Value 40 Size 4
     Call 'CBL_TOUPPER'     using UPPER-AREA by Value 40 Size 4
     If MIXED-AREA = LOWER-AREA and not = UPPER-AREA
         Move LOWER-AREA       to MIXED-AREA
         Perform Varying MX  from MP by -1 Until MX < 1
             If MA(MX) >= "a" and <= "z"
             and (MX < 2 or MA(MX - 1) < "a" or > "z")
                 Subtract 32 from MN(MX)
             End-If
             If MX < 39 and (MIXED-AREA(MX:3) = '1St' or '2Nd' or '3Rd'
                             or MIXED-AREA(MX + 1:2) = 'Th'
                             and (MA(MX) >= '4' and <= '9' or = Zero))
             or MX < 40 and MIXED-AREA(MX:3) = "'S" and (MX = 39
             or MA(MX + 2) < "a" or > "z")
                 Add 32        to MN(MX + 1)
             End-If
             If MX < 39
             and MIXED-AREA(MX:2) = "Mc" and MA(MX + 2) >= "a" and <= "z"
                 Add 32        to MN(MX + 2)
             End-If
         End-Perform
     End-If
     Move MIXED-AREA           to PARM-1(1:MP)
     .
 
Move PARM-1(1:MP) to MIXED-AREA
I'm not sure a Cobol74 compiler admits that.
 
You are correct, PHV. Unfortunately, "Reference Modification" did not become available until COBOL85.

Fab
 
inspect.... converting is the right and fast statement.

old statements are also examine and transform.

Regards,

Crox
 
Crox, didn't INSPECT ... CONVERTING become available with cobol85 ?
 
PHV said:
[D]idn't INSPECT ... CONVERTING become available with cobol85?

Correct. As far as ANSI/ISO standard COBOL, INSPECT CONVERTING made its first appearance in the 1985 standard. The INSPECT statement with TALLYING and REPLACING first appeared in the 1974 standard.

Tom Morrison
 
On the IBM mainframe, I seem to recall that there was a TRANSFORM verb in the 74 compiler.
 
right. Transform is the old statement to do the same thing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top