title: String Incrementing Function in C
body: <p>For my programming class, I am trying to write a function incrementstring() that takes the string 'str' passed in from a driver, and adds one to them. It should work with both letters and numbers (ex. '1' goes to '2', 'a' goes to 'b', 'z' goes to 'aa', 'ZZ' goes to 'AAA'). I have almost every test condition working, except for one bug that I can't seem to find a way around.</p>
  
  <p>This is what I currently have:</p>
  
  <pre><code>void incrementstring(char* str){
  int i;
  int j;
  int length = strlen(str);
  for(i = strlen(str)-1; i &gt;= 0; i--){
      if (str[i] == '9'){
              str[i] = '0';
              if (str[0] == '0'){
                      for (j = strlen(str)-1; j&gt;=0; j--){ //This loop is the problem
                              str[j+1] = str[j];
                              }
                      str[0] = '1';
                      }
      }
      else if (str[i] == 'z'){
              if (str[0] == 'z'){
                      str[i] = 'a';
                      str[i+1] = 'a';
                      }
              str[i] = 'a';
              }
  
      else if (str[i] == 'Z'){
              if(str[0] == 'Z'){
                      str[i] = 'A';
                      str[i+1] = 'A';
              }
              str[i] = 'a';
      }
      else{
              str[i]++;
              return;
      }
  
  }
  }
  </code></pre>
  
  <p>When I run the function, this is what the driver outputs:</p>
  
  <pre><code> 1. testing "1"... = 2. Correct!
   2. testing "99"... = 100. Correct!
   3. testing "a"... = b. Correct!
   4. testing "d"... = e. Correct!
   5. testing "z"... = INCORRECT: we got "aa0". We should be getting "aa" instead.
   6. testing "aa"... = ab. Correct!
   7. testing "Az"... = Ba. Correct!
   8. testing "zz"... = aaa. Correct!
   9. testing "cw"... = cx. Correct!
   10. testing "tab"... = tac. Correct!
   11. testing "500"... = 501. Correct!
  
  11 tests run.
  </code></pre>
  
  <p>I wrote a for loop in line 9 to handle the '99' to '100' condition. It takes every index of the string and shifts it one to the right, and then adds a '1' to the beginning of the string. However, this loop for some reason messes up the 5th test condition, as seen above. If I take the loop out, '99' will go to '00', but the 5th test will pass with no problems. I've hit a brick wall here and I was wondering if anybody can provide some insight.</p>
  
  <p>I appreciate the help, thanks.</p>