Looks like a similar thing was proposed a while back: Titlecase function
Maybe you can make use of their implementation?