An approach using LaTeX3: the important command is \regex_replace_all:nnN. Its first argument is a regular expression (here, [A-Z] matches any uppercase letter); its second argument the replacement, here \textit followed by \0 (what the regular expression matched); and the third is a token list variable on which we want to do the replacement.

Of course, spaces are preserved, as are any formatting commands within the argument (here I put \texttt for the demo). This should work with any recent enough version of the l3kernel and l3experimental bundles (February 2012, say).

I changed the regex to match any number of consecutive uppercase letters, and I changed the replacement text to add braces around the argument to \textit. This avoids the extra space inserted in \textit{N}\textit{T}. The output:

Thanks a lot for your LaTeX3 code. I like to see how a problem can be solved with different kinds of LaTeX. Maybe in near future it should be good practice to show how one can solve a problem with different kinds of LaTeX like it is shown on this question.
–
HolleFeb 13 '12 at 20:19

Ok, here is an other approach dedicated to LuaLaTeX fans (and future fans of LuaLaTeX). I think it is a good example to show how easy it is to write a few (easy to understand) lines of Lua code. In the provided Lua code one can check the input string for every char and format any LaTeX string you need without cryptic TeX commands.

It is good practice to write the lua functions in a separate file with the extension .lua. For this MWE I use the filecontents environment instead to provide an extra file for the lua script.

Edit: After the great comment from Khaled Hosny I want to provide a very straight solution. It shows again the power of Lua ;-). The new function replaces all (thats what the + after %u stands for) upper chars (thats what the %u stands for) with \emph{}. The %1 holds the value which is surrounded by the ( ) in the search pattern.

I use it in my document to format transliterations from semitic/arab languages. (instead of the \emph command i use the relsize package to make the capitals 15% smaller, and i format the whole thing with a different font)

Of course you'll have noticed that only the first letter is italicized, provided it's uppercase. This code is wrong, because it only examines the first letter. The cause, of course, is the double L.
–
egregFeb 15 '12 at 10:03

Swapping #2 and \relax would help a bit.
–
Bruno Le FlochMar 1 '12 at 2:48