title: How to capitalize every other letter in a string efficiently in java?
body: <p>I'm currently working on a problem in code hunt level 6.02 which asks me to capitalize every other letter in a String. I have tried doing it with toCharArray + StringBuilder in for loops. It works, but it's not good enough. I still can't get the perfect score for the problem. I'm running out of ideas. Any help will be greatly appreciated. </p>
  
  <p>Note: This is my first post on stack overflow. So if I miss anything or ask question in a wrong way. Pls feel free to point it out for me. Thx. </p>
  
  <p>s is the input string</p>
  
  <p>Attempt 1:</p>
  
  <pre><code>    char [] words = s.toCharArray();
      for (int i = 0; i &lt; words.length; i +=2){
          words[i] = Character.toUpperCase(words[i]);
      }
      return new String(words);
  </code></pre>
  
  <p>Attempt 2:    </p>
  
  <pre><code>    StringBuilder result = new StringBuilder(s);
      for (int i = 0; i &lt; result.length(); i +=2){
          result.replace(i, i + 1, result.substring(i,i + 1).toUpperCase());
      }
      return result.toString();
  </code></pre>
  
  <p>Input: "iaiaa"</p>
  
  <p>Expected output: "IaIaA"</p>