You just need to make sure that the same character is never swapped to
the same position twice.  Here is one way to do it.

#include <stdio.h>
#include <string.h>

void swap(char *s, int i, int j)
{
  char t = s[i];
  s[i] = s[j];
  s[j] = t;
}

void permute(char *s, int n)
{
  if (s[n]) {
    int i;
    unsigned char done[256] = { 0 };
    for (i = n; s[i]; i++) {
      if (!done[s[i]]) {
        swap(s, n, i);
        permute(s, n + 1);
        swap(s, n, i);
        done[s[i]] = 1;
      }
    }
  }
  else printf("%s\n", s);
}

int main(int argc, char *argv[])
{
  char buf[10 * 1024];
  if (argc > 1) {
    strcpy(buf, argv[1]);
    permute(buf, 0);
  }
  return 0;
}

On May 7, 6:23 am, Sairam <ravu...@gmail.com> wrote:
> Thanks for ur clean Code!! But you haven't considered the case of repeating
> characters in a string

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to