x <- c("h a g wells j g verne", "h a g wells j g")
gsub("(?<=\\b\\w) (?=\\w\\b)|(?<=\\b\\w)(?= |$)", ".", x, perl=TRUE)