home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a013 / 1.ddi / SOURCE.EXE / F_CAPFIR.PRG < prev    next >
Encoding:
Text File  |  1991-01-25  |  2.0 KB  |  75 lines

  1. *****************************************************************
  2. FUNCTION CAPFIRST (string)
  3. *****************************************************************
  4.  
  5. * Capitalize the first letter of each word in a string
  6.  
  7. * Copyright(c) 1991 -- James Occhiogrosso
  8.  
  9.  
  10.  
  11. LOCAL position := 0, punct_mark := '.,-"',  ;
  12.       ret_string := temp_word := word := ''
  13.  
  14. DO WHILE LEN(string) > 0
  15.  
  16.     * Extract a word from the passed string
  17.     word = PARSE(@string)
  18.  
  19.     * Capitalize first letter of word
  20.     word = UPPER(SUBSTR(word,1,1)) + LOWER(SUBSTR(word,2))
  21.  
  22.     * Test for period, comma, dash or double quotation mark
  23.     FOR counter = 1 TO 4
  24.          temp_word = ''
  25.  
  26.          * If any exist, get position in word
  27.          position = AT(SUBSTR(punct_mark, counter, 1), word)
  28.  
  29.          DO WHILE position > 0
  30.  
  31.              * Capitalize first letter after punctuation mark
  32.              temp_word = temp_word + SUBSTR(word, 1, position) +;
  33.                          UPPER(SUBSTR(word, position + 1, 1))
  34.  
  35.              * And trim original word
  36.              word = SUBSTR(word, position + 2)
  37.  
  38.              * Are there any more of the same mark in string?
  39.              position = AT(SUBSTR(punct_mark, counter, 1), word)
  40.  
  41.              IF position = 0
  42.                  * If not, we are done. Otherwise, loop
  43.                  word = temp_word + word
  44.                  EXIT
  45.              ENDIF
  46.  
  47.          ENDDO
  48.     NEXT
  49.  
  50.     * Test for an apostrophe
  51.     position = AT("'", word)
  52.  
  53.     IF position > 0
  54.  
  55.          * If one exists capitalize first letter after it if
  56.          * it follows a capitol letter. (Names like "O'Brian")
  57.  
  58.          IF ISUPPER(SUBSTR(word, position-1))
  59.              word = SUBSTR(word, 1, position)            + ;
  60.                     UPPER(SUBSTR(word, position + 1, 1)) + ;
  61.                     SUBSTR(word, position + 2)
  62.          ENDIF
  63.     ENDIF
  64.  
  65.     * Add word to return string
  66.     ret_string = ret_string + word + ' '
  67.  
  68. ENDDO
  69.  
  70. * Set local string equal to return string
  71. string = ret_string
  72.  
  73. RETURN ret_string
  74.  
  75.