i have a suggestion

try separating each word into a different char[] u can use a for loop combined with an if to check for space character (ascii 32) then it should be a piece of cake just compare and add x where they reapeat