title: Capitalize various letters in a string
body: <p>I am working on an interview question:</p>
  
  <blockquote>
    <p>Capitalize 2nd, 4th, 8th, 16th letters in a string </p>
    
    <p>input - "letters in a string" </p>
    
    <p>output - "lEtTers in a stRing"</p>
  </blockquote>
  
  <p>Here is my solution:</p>
  
  <pre><code>public static void main(String[] args) {
      String input = "letters in a string";
  
      String output = input.substring(0, 1) + input.substring(1, 2).toUpperCase() + input.substring(2, 3)
              + input.substring(3, 4).toUpperCase() + input.substring(4, 7) + input.substring(7, 8).toUpperCase()
              + input.substring(8, 15) + input.substring(15, 16).toUpperCase() + input.substring(15);
      System.out.println(output);
  
  }
  </code></pre>
  
  <p>Is there any way we can generalize this without hard coding the offset numbers here? In general, what I am looking for is - given the letter number which we want to capitalize, our program should work on that without changing the core logic and should be efficient as well in terms of complexity?</p>