title: Replacing character within cstring - getting bad access
body: <p>Is it possible to replace a character from a c string after converting it from NSString via the UTF8string method?</p>
  
  <p>For example take the code below. It is to format a string with particular rule.</p>
  
  <pre><code>- (NSString *)formatString:(NSString *)input {
      if (input.length==0) {
          return @"";
      }
      //code to determine rule
      ....
      ....
      // substitute output format with input characters
      if (rule) {
          input = [input substringFromIndex:prefix.length];
          char *string = (char *)[rule UTF8String];
          int repCount = 0;
          for (int i=0; i&lt;rule.length; i++) {
              if (string[i] == '#') {
                  if (repCount &lt; input.length) 
                      string[i] = [input characterAtIndex:repCount++];//bad access
                  else 
                      string[i] = ' ';
              }
          }
          NSMutableString *output = [NSMutableString stringWithCString:string encoding:NSUTF8StringEncoding];
       ...
       ... //do something with the output 
      return output;
      } else {
      return input;
      }
  }
  </code></pre>
  
  <p>Initially string[0] has '#' and it should get replaced with the character in the input. This is not happening.</p>