title: How can I find all permutations of a string without using recursion?
body: <p>Can someone help me with this: This is a program to find all the permutations of a string of any length. Need a non-recursive form of the same. ( a C language implementation is preferred)</p>
  
  <pre><code>using namespace std;
  
  string swtch(string topermute, int x, int y)
  {
    string newstring = topermute;
    newstring[x] = newstring[y];
    newstring[y] = topermute[x]; //avoids temp variable
    return newstring;
  }
  
  void permute(string topermute, int place)
  {
    if(place == topermute.length() - 1)
    {
      cout&lt;&lt;topermute&lt;&lt;endl;
    }
    for(int nextchar = place; nextchar &lt; topermute.length(); nextchar++)
    {
      permute(swtch(topermute, place, nextchar),place+1);
    }
  }
  
  int main(int argc, char* argv[])
  {    
    if(argc!=2)    
    {
      cout&lt;&lt;"Proper input is 'permute string'";
      return 1;
    }
    permute(argv[1], 0);
    return 0;    
  }
  </code></pre>