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

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12313
  • 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: 1747
  • 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: 12313
  • 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: 12313
  • 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)
    )
  )
)

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9297
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).
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

roy_043

  • Water Moccasin
  • Posts: 1747
  • 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: 12313
  • 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: 12313
  • 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.

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9297
Re: -={ Challenge }=- Titlecase
« Reply #8 on: October 08, 2010, 04:38:34 PM »
A little crabby are we?
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

Lee Mac

  • Seagull
  • Posts: 12313
  • 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.

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9297
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?
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

roy_043

  • Water Moccasin
  • Posts: 1747
  • 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: 10376
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.

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9297
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 »
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

David Bethel

  • Swamp Rat
  • Posts: 641
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