Author Topic: -={ Challenge }=- Titlecase  (Read 19488 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
-={ Challenge }=- Titlecase
« on: October 08, 2010, 03:23:32 PM »
The Challenge:

To construct a function to return a string converted to titlecase.

Code: [Select]
(Titlecase <string>)
Examples:

Code: [Select]
_$ (Titlecase "test string")
"Test String"
Code: [Select]
_$ (Titlecase "TEST")
"Test"
Code: [Select]
_$ (Titlecase "TEST STRING")
"Test String"

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: -={ Challenge }=- Titlecase
« Reply #1 on: October 08, 2010, 03:32:24 PM »
There is titlecase and titlecase...
Should it be:
"The Taming Of The Shrew"
Or:
"The Taming of the Shrew"
?

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: -={ Challenge }=- Titlecase
« Reply #2 on: October 08, 2010, 03:38:23 PM »
To remove ambiguity, the first one Roy  :-)

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: -={ Challenge }=- Titlecase
« Reply #3 on: October 08, 2010, 03:46:12 PM »
To kick us off perhaps :-)

Code: [Select]
(defun LM:TitleCase ( s / n )
  (vl-list->string
    (mapcar
      (function
        (lambda ( x )
          (setq n (boole (if (or (not n) (= 32 n)) 2 7) x 32))
        )
      )
      (vl-string->list s)
    )
  )
)

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: -={ Challenge }=- Titlecase
« Reply #4 on: October 08, 2010, 04:08:38 PM »
To remove ambiguity, the first one Roy  :-)

read: easier to program. *lol*

Didn't we already have one like this?


EDIT: Sorry, that was "split by case" (I found a function in my junk file).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: -={ Challenge }=- Titlecase
« Reply #5 on: October 08, 2010, 04:08:49 PM »
Code: [Select]
(LM:TITLECASE "it's easy as 123")

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: -={ Challenge }=- Titlecase
« Reply #6 on: October 08, 2010, 04:12:29 PM »
Code: [Select]
(LM:TITLECASE "it's easy as 123")

Nice spot Roy.
Code: [Select]
(defun LM:TitleCase2 ( s / n )
  (vl-list->string
    (mapcar
      (function
        (lambda ( x )
          (setq n
            (if (or (not n) (= 32 n))
              (if (< 96 x 123) (boole 2 x 32) x)
              (if (< 64 x  91) (boole 7 x 32) x)
            )
          )
        )
      )
      (vl-string->list s)
    )
  )
)

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: -={ Challenge }=- Titlecase
« Reply #7 on: October 08, 2010, 04:16:56 PM »
To remove ambiguity, the first one Roy  :-)

read: easier to program. *lol*

whatever, I'll let you do the other one then.

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: -={ Challenge }=- Titlecase
« Reply #8 on: October 08, 2010, 04:38:34 PM »
A little crabby are we?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: -={ Challenge }=- Titlecase
« Reply #9 on: October 08, 2010, 04:44:33 PM »
A little crabby are we?

I've got better things to do than rise to your snide comments.

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: -={ Challenge }=- Titlecase
« Reply #10 on: October 08, 2010, 04:47:06 PM »
*blink* WHAT?! How was my comment `snide'?
...you got a problem with me?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: -={ Challenge }=- Titlecase
« Reply #11 on: October 08, 2010, 04:50:15 PM »
A titlecase-function will always be language dependent:
Code: [Select]
(LM:TITLECASE2 "McMurphy is an Irish-American brawler")http://en.wikipedia.org/wiki/Randle_McMurphy

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: -={ Challenge }=- Titlecase
« Reply #12 on: October 08, 2010, 05:28:30 PM »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: -={ Challenge }=- Titlecase
« Reply #13 on: October 08, 2010, 10:19:49 PM »
Code: [Select]
(defun J7:TitleCase ( s / )
  (setq s (strcase s T))
  (while (setq sp (vl-string-position 32 s sp))
         (setq s (vl-string-subst
                   (chr (boole 2 (vl-string-elt s (1+ sp)) 32))
                   (chr (vl-string-elt s (1+ sp)))
                   s
                   (1+ sp)
                   )
               sp (1+ sp)
               )
         )
  (vl-string-subst
    (chr (boole 2 (vl-string-elt s 0) 32))
    (chr (vl-string-elt s 0))
    s
    )
 )


EDIT: Fixed formatting of code (code not changed).
« Last Edit: October 08, 2010, 10:34:16 PM by Se7en »
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

David Bethel

  • Swamp Rat
  • Posts: 656
Re: -={ Challenge }=- Titlecase
« Reply #14 on: October 09, 2010, 05:58:18 AM »
Very old
Code: [Select]
;;;PROPER NAMES
(defun proper (s / i tmp)
  (setq i 1)
  (while (<= i (strlen s))
          (cond ((= i 1)
                 (setq tmp (strcase (substr s i 1))))
                ((= " " (substr s (1- i) 1))
                 (setq tmp (strcat tmp (strcase (substr s i 1)))))
                (T
                 (setq tmp (strcat tmp (strcase (substr s i 1) t)))))
           (setq i (1+ i)))
tmp)
-David
R12 Dos - A2K