title: Heap's algorithm in C++ repeating?
body: <p>I am trying to implement Heap's algorithm in C++.<br />
  However, the algorithm starts to repeat if the string it's permuting gets to a length of 4. Here's the code:</p>
  <pre><code>void permute(int n, string str, int *total,string array[]){
  
      if (n==0){
          
          array[*total] = str;
          *total += 1;
      
      }
  
      else{
      
          for(int c=0; c&lt;=n;c++){
              
              permute(n-1,str,total, array);
              if (n % 2 == 0){
                  char tmpstr=str[c];
                  str[c]=str[n];
                  str[n]=tmpstr; 
                  }
              else{
                  char tmpstr=str[0];
                  str[0]=str[n];
                  str[n]=tmpstr;
              }
          }
      }
  }
  
  int main() {
      int total = 0;
      string array[24];
      permute(3,&quot;abcd&quot;,&amp;total, array);
      cout &lt;&lt; total &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  <p>And here is the output. It repeats at the 13th line</p>
  <pre><code>24
  abcd
  bacd
  cbad
  bcad
  cabd
  acbd
  dbca
  bdca
  cbda
  bcda
  cdba
  dcba
  abcd &lt;-- right here
  bacd
  cbad
  bcad
  cabd
  acbd
  dbca
  bdca
  cbda
  bcda
  cdba
  dcba
  </code></pre>
  <p>Thank you guys and any help is welcome!</p>