title: How to preserve the punctuation when converting words to Pig Latin?
body: <p>I've been working on a Java program to convert English words to Pig Latin. I've done all the basic rules such as appending -ay, -way, etc., and special cases like question -> estionquay, rhyme -> ymerhay, and I also dealt with capitalization (Thomas -> Omasthay). However, I have one problem that I can't seem to solve: I need to preserve before-and-after punctuation. For example, What? -> Atwhay? Oh!->Ohway! "hello" -> "ellohay" and "Hello!" -> "Ellohay!" This is not a duplicate by the way, I've checked tons of pig latin questions and I cannot seem to figure out how to do it. </p>
  
  <p>Here is my code so far (I've removed all the punctuation but can't figure out how to put it back in):</p>
  
  <pre><code>public static String scrub(String s)
  {
   String punct = ".,?!:;\"(){}[]&lt;&gt;";
   String temp = "";
   String pigged = "";
   int index, index1, index2, index3 = 0;
  
   for(int i = 0; i &lt; s.length(); i++)
   {
    if(punct.indexOf(s.charAt(i)) == -1) //if s has no punctuation
    {
     temp+= s.charAt(i);
    }
   } //temp equals word without punctuation
   pigged = pig(temp); //pig is the piglatin-translator method that I have already written, 
   //didn't want to put it here because it's almost 200 lines
  
   for(int x = 0; x &lt; s.length(); x++)
   {
    if(s.indexOf(punct)!= -1)//punctuation exists
     {
      index = x; 
     }
   }
  }
  </code></pre>
  
  <p>I get that in theory you could search the string for punctuation and that it should be near the beginning or end, so you would have to store the index and replace it after it is "piglatenized", but I keep getting confused about the for loop part. if you do index = x inside the for-loop, you're just replacing index every time the loop runs. 
  Help would be appreciated greatly! Also, please keep in mind I can't use shortcuts, I can use String methods and such but not things like Collections or ArrayLists (not that you'd need them here), I have to reinvent the wheel, basically. <strong>By the way, in case it wasn't clear, I already have the translating-to-piglatin thing down. I only need to preserve the punctuation before and after translating.</strong></p>