Question: I currently have a program that individually converts tokens of a string into their piglatin counterparts. However, the program needs to insert them back into the string they were taken with, with ALL of the original characters in it.

```
Hasta la vista baby. - the Terminator.

Hasta
astaHay

la
alay

vista
istavay

baby
abybay

the
ethay

Terminator
erminatorTay

```

These are all of the words and their conversions. I tried a method directly placing them back in, however accounting for missing characters and different length made it hard for me to do that. I tried to insert characters based on the length of each token added up, but that ran into complications when there were more than 1 whitespace character. How would I insert these words back into the string so it looks like this:

```
Astahay alay istavay abybay. - ethay Erminatortay

    PigOrig = key.readLine();
    String[] PigSplit = PigOrig.split("\\W+");
    for(int i = 0; i < PigSplit.length; i++)
    {
        if(PigSplit[i] != null)
        {
            FinalStr += Piggy.vowelOut(PigSplit[i]); // VowelOut returns the converted word only, no trailing whitespace or punctuation
            lengthtot += PigSplit[i].length();
            FinalStr += PigOrig.charAt(lengthtot); // attempt at adding up the words and inserting the original punctuation that was in the string PigOrig
            lengthtot ++;
         }

    }

```

Answer: 
If I understand your question, it is 'how do I replace each word with its translation in a string?' The simplest way is to use `String.replace`.

So if you have created a `translate` method then you could do something like:

```
String line = key.readLine();
for (String word: line.split("\\W+"))
    line = line.replace(word, translate(word));

```

The advantage of this approach is that you are replacing the words in the original string not putting the words back together again.

Also note that it might be easier to translate just using pattern matching. For example:

```
private String translate(String word) {
    Matcher match = Pattern.compile("(\\w*)([aeiou]\\w*)").match(word);
    if (match.matches())
        return match.group(2) + match.group(1) + "ay";
    else
        return word;
}

```
If I understand correctly that you want to translate all the words in the input, my taste would be for building the new string from scratch:

```
    String pigOrig = key.readLine();
    String[] pigSplit = pigOrig.split("\\W+");
    StringBuilder buf = new StringBuilder(pigOrig.length());
    buf.append(translateWord(pigSplit[0]));
    for(int i = 1; i < pigSplit.length; i++) {
        buf.append(' ');
        buf.append(translateWord(pigSplit[i]));
    }
    String result = buf.toString();

```