title: How to replace a repeated char with some characters or string
body: <p>I need to replace a repeated char with <code>$%</code> followed by the char followed by <code>$%</code>. </p>
  
  <p>e.g. "HELLO" will become "HE$%L$%O"</p>
  
  <p>The following code that I wrote gives "HE$%L$%LO".</p>
  
  <p>Please guide</p>
  
  <pre><code>    int index=0;
      String str1="";
      String str2="";
      String str4="";
      String str5="";
  
      for(int i=0;i&lt;str.length();i++) {
  
          char ch=str.charAt(i);
          index=str.indexOf(ch);
          if(index!=i) {
  
              str4="$%"+str.charAt(index)+ "$%";
              str1=str.charAt(index)+str5;
  
              str2=str.replaceFirst(str1,str4);
  
          }
      }
      return str2;
  </code></pre>