Created On:  16 November 2012

Problem:

Customer is using Net Express 5.1 and has the following question.

Net Express has the instrinsic functions upper-case and lower-case. Is there a Function or Module that converts string fields into upper AND lowercase?

For example: I would like to convert "JOHN DAVID SMITH" into "John David Smith".

Resolution:

There is no COBOL intrinsic function to do this mixed case conversion but here is a simple demo to show you how you can write one in COBOL.

This example is also attached to this article as a zip file.

The mixedcase function will capitalize all words in a string passed to it.

Calling program:

      *----------------------------------------------------------------*
      *                         Test Case   
      *                                                                
      * This example program demonstrates how to create a new intrinsic
      * function in Net Express that will convert a full name passed in
      * a string as uppercase to a mixed case where each name in the
      * string will appear with an initial capital letter and all      
      * other characters will be set to lowercase.                    
      *                                                                
      * This function will then be callable like other functions       
      * upper-case and lower-case.                                     
      *----------------------------------------------------------------*
      $set preservecase case
       id division.
       program-id.   testcase.
       environment division.
       repository.
           function mixedcase.

       data division.
       working-storage section.
       01 my-string     pic x(256) value "JOHN DAVID SMITH".
       procedure division.

           move function mixedcase(my-string) to my-string
           display my-string
           stop run.

Function mixedcase:

      *----------------------------------------------------------------*
      *                     Mixed Case                                 
      *                                                                
      * This function take a 256 byte string as a parameter containing 
      * one or more parts of a person's name in UPPER-CASE. It will    
      * convert each word in the string to intial caps with the rest in
      * lower-case.                                                    
      *----------------------------------------------------------------*
      $set repository(update ON) preservecase case
       function-id. mixedcase.
       working-storage section.
       01 sub-1         pic 9(3).
       01               pic x      value "N".
          88 space-found           value "Y"
             when set to false           "N".
       linkage section.
       01 my-string     pic x(256).
       01 new-string    pic x(256).
       procedure division using my-string
                          returning new-string.

           move function lower-case(my-string) to new-string
           set space-found to true
           perform varying sub-1 from 1 by 1
              until sub-1 > function length(new-string)
                 if new-string(sub-1:1) not = " "
                    if space-found
                       move function upper-case(new-string(sub-1:1))
                          to new-string(sub-1:1)
                       set space-found to false
                    end-if
                 else
                    set space-found to true
                 end-if
           end-perform
           goback.

       end function mixedcase.