Capitalize First Letter function

Cecil

19+ years progress programming and still learning.
Hi all.

I need to Capitalize First Letter of a persons name. Sounds easy.... But I have name in our database which are like Tim MacDonald, Brian O'Reilly and Peter von Hook.

Has any one written a function which handles First letter Capitalize the first letter of a person's name and handles weird names. Not the I am calling Tim MacDonald weird.

Thanks.
 

dayv2005

Member
try this

Code:
RETURNS CHARACTER
    ( INPUT iString AS CHARACTER ) :
    
    DEFINE VARIABLE iWordNum      AS INTEGER    NO-UNDO.
    DEFINE VARIABLE cWord         AS CHARACTER  NO-UNDO.
    DEFINE VARIABLE cSep          AS CHARACTER  NO-UNDO.
    DEFINE VARIABLE cOutputString AS CHARACTER  NO-UNDO.

    /* I assign this globally now */

    /*ASSIGN
        CaseList = "AL,AK,AZ,AR,CA,CO,CT,DE,FL,GA,HI,ID,IL,IN,IA,KS,KY,LA,ME,MD,"
        CaseList = CaseList + "MA,MI,MN,MS,MO,MT,NE,NV,NH,NJ,NM,NY,NC,ND,OH,OK,"
        CaseList = CaseList + "OR,PA,RI,SC,SD,TN,TX,UT,VT,VA,WA,WV,WI,WY".*/

    /* All is lowercase, unless specified otherwise */
    iString = LC(iString).

    /* Loop thru the words of the sentence */
    DO iWordNum = 1 TO NUM-ENTRIES( iString,' ' ):

        /* Pick a word */
        cWord = ENTRY(iWordNum,iString,' ').

        /* Capitalize first letter of a word */
        IF iWordNum = 1 OR (NOT ( LENGTH(cWord) < 1 OR CAN-DO('the,and,from',cWord))) THEN
            OVERLAY(cWord,1,1) = CAPS(SUBSTRING(cWord,1,1)).

        IF CAN-DO(CaseList, cWord) THEN
            cWord = CAPS(cWord).

        /* Build the output string */
        cOutputString = cOutputString + cSep + cWord.
        cSep = CHR(32).
    END.

    RETURN cOutputString.

END FUNCTION.
 

Cecil

19+ years progress programming and still learning.
Does not Quite work the way I was hoping. Thanks you any way.
 

gean melo

New Member
this is can help you
try it,

function f-Capitalize returns char (input inp-name as char ):

def var i-cont as int no-undo.
def var v-output as char no-undo.

if inp-name = "" then return "" .

do i-cont = 1 to num-entries(inp-name," "):

assign v-output = v-output +
caps(substring(entry(i-cont,inp-name," "),1,1)) + substring(entry(i-cont,inp-name," "),2,length(entry(i-cont,inp-name," "))) + " " .
end.

return v-output .

end function.

display f-Capitalize("gean melo de oliveira") format "x(50)" .

the return of this function is ===> Gean Melo De Oliveira .

- this code is more simple you can add more features that can be meet their needs .
 

dayv2005

Member
Does not Quite work the way I was hoping. Thanks you any way.

Welcome. This was a basic function that would capitalize everything after a space (first letter only) then i needed to mod it for customer locations and addresses that why i added the state thing to it. Also then i added some other little mods. But the previous thread looks like he has something that will fit your needs.
 

gean melo

New Member
I am not stating this function.

put a simple code for that starting point that it poses to add more features to meet their needs.

because your function not possibly enjoy it because he did not understand.

and if you did not understand he just wants a function to capitalize the first letters of names but that meet such needs

" I need to Capitalize First Letter of a persons name. Sounds easy.... But I have name in our database which are like Tim MacDonald, Brian O'Reilly and Peter von Hook. " .
 

TomBascom

Curmudgeon
There is no general rule that will allow you to programatically and correctly capitalize names such as:

But I have name in our database which are like Tim MacDonald, Brian O'Reilly and Peter von Hook.

You could probably get a fairly good "hit ratio" from a relatively small set of simple rules but you will need to allow for manual override when somebody needs an exception.
 

Cecil

19+ years progress programming and still learning.
It's been a while since I started this thread. But I have finally written a function which will do first letter capitalisation for persons names. It's not 100% perfect but it does work well for my needs. Please feel free to use/improve it.

The function test for three things. 1. A name which is equal to an exclude list. 2. A name which begins with a partial name. 3 A name which is double-barreled.

Code:
FUNCTION NameCaps RETURNS CHARACTER
  (INPUT pcName AS CHARACTER):

  /* Function to capitilise the first letter of a given name. */

  &SCOPED-DEFINE xcBegins   "Mc,Mac,M',O'" 
  &SCOPED-DEFINE xcContains "-"
  &SCOPED-DEFINE xcExclude  "von,van,de,di,der,ter"
  &SCOPED-DEFINE xcSpace  " "
  
  DEFINE VARIABLE chNameBegins    AS CHARACTER   NO-UNDO.
  DEFINE VARIABLE chNameElement   AS CHARACTER   NO-UNDO.
  DEFINE VARIABLE chDoubleName    AS CHARACTER   NO-UNDO.
  DEFINE VARIABLE inLoop          AS INTEGER     NO-UNDO.
  DEFINE VARIABLE inInnerLoop     AS INTEGER     NO-UNDO.
  DEFINE VARIABLE inNameLegnth    AS INTEGER     NO-UNDO.
  DEFINE VARIABLE inNameElements  AS INTEGER     NO-UNDO.
  DEFINE VARIABLE inSecondCapPos  AS INTEGER     NO-UNDO.
  
  /* Covert the name to lower case.*/
  ASSIGN
    pcName = LC(pcName).
  /* Get the number of name elements speperated by a space */
  ASSIGN
    inNameElements = NUM-ENTRIES(pcName, {&xcSpace}).
  /* Loop through each name element one-by-one*/
  NAME-LOOP:
  DO inLoop = 1 TO inNameElements:
    /* Extract each name out of the name elemement string.*/
    ASSIGN
      chNameElement = ENTRY(inLoop, pcName, {&xcSpace}).
    /* Test to see if the name is in the exclude list. Move on 
       if the result returns true. */
    IF LOOKUP(chNameElement, {&xcExclude}) GT 0 THEN
      NEXT NAME-LOOP.
    /* Capitalise the first letter of the name element.*/
    SUBSTRING(chNameElement,1,1) = CAPS(SUBSTRING(chNameElement,1,1)).
    /* Insert the captilized name back into the name element string.*/
    ENTRY(inLoop, pcName, {&xcSpace}) = chNameElement.
    /* Test the name element for names begining with partial name. */
    DO inInnerLoop = 1 TO NUM-ENTRIES({&xcBegins}):
      /* Extract partial name from the begins list.*/
      ASSIGN
        chNameBegins = ENTRY(inInnerLoop,{&xcBegins}).
      /* Test the name element to see if it begins with a partial name...*/
      IF chNameElement BEGINS chNameBegins THEN
      DO:
        /* Get the position of the charater that needs to be 
           capitialsed by obtaining the length of partial name.. */
        ASSIGN 
          inSecondCapPos = LENGTH(chNameBegins) + 1.
        SUBSTRING(chNameElement,inSecondCapPos,1) = CAPS(SUBSTRING(chNameElement,inSecondCapPos,1)).
        /* Insert the captilized name back into the name element string.*/
        ENTRY(inLoop, pcName, {&xcSpace}) = chNameElement.
      END.
    END.
    /* Test the name element for double barreled names. */
    DO inInnerLoop = 1 TO NUM-ENTRIES({&xcContains}):
            /* Extract name partical from the begins list.*/
      ASSIGN
        chDoubleName = ENTRY(inInnerLoop,{&xcContains}).
        inSecondCapPos = INDEX(chNameElement, chDoubleName).
      /* Test if the name is a double barred name.*/
      IF inSecondCapPos GT 0 THEN
      DO:
        /* Get the position of the charater that needs to be capitialsed 
           by obtaining the position on the name speperator.. */
        ASSIGN 
          inSecondCapPos = inSecondCapPos + 1.
        SUBSTRING(chNameElement,inSecondCapPos,1) = CAPS(SUBSTRING(chNameElement,inSecondCapPos,1)).
        /* Insert the captilized name back into the name element string.*/
        ENTRY(inLoop, pcName, {&xcSpace}) = chNameElement.
      END.
    END.
  END.
  
  RETURN pcName.  /*  Return the new name.....*/

END FUNCTION.
  
MESSAGE NameCaps("james von brun-dell macDonaled mcdougle peter o'tool").
 
Top