This will only operate on
\w characters, and I took the requirement of the OPer's homework problem to be to operate on
any character. Also, it operates on substrings, and I understood the requirement to be to operate on the entire string.
>perl -le "$_=shift; s/\b(\w)(\w*)(\w)\b/$3$2$1/g; print" *%$#
*%$#