on-internet
← Community

How can I convert text to title case?

Stack Overflow ·

I have a text file containing a list of titles that I need to change to title case (words should begin with a capital letter except for most articles, conjunctions, and prepositions). For example, this list of book titles: barbarians at the gate hot, flat, and crowded A DAY LATE AND A DOLLAR SHORT THE HITCHHIKER'S GUIDE TO THE GALAXY should be changed to: Barbarians at the Gate Hot, Flat, and Crowded A Day Late and a Dollar Short The Hitchhiker's Guide to the Galaxy I wrote the following code: while(<DATA>) { $_=~s/(\s+)([a-z])/$1.uc($2)/eg; print $_; } But it capitalizes the first letter of e

Comments

Sign in or sign up to comment and upvote.

No comments yet.