/*  funcija natisne vse permutacije string "array"  (ki je dolg "len"), ki se zacnejo s "start."*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void permute(char *array, int start, int len)
{
int j;
char    *s;
if(start < len)
  {
    if(NULL == (s = malloc(len)))
    {
      printf("\n\nMemory error!!!\a\a\n");
      abort();
    }
    strcpy(s, array);
    for(j=start ; j<len ; ++j)
    {  
      int     temp;
	  if((j == start) || (s[j] != s[start]))
	  {     /* For each character that's different    */
		/* Swap the next first character with...  */
		/* the current first        */
		temp = s[j];
		s[j] = s[start];
		s[start] = temp;
		permute(s, start+1, len);
	  }
    }
    free(s);
  }
  else
    puts(array);
}

int charcmp(char *a, char *b)
{
      return(*a - *b);
}

main(){
  char *beseda = "BANANA";
  permute(beseda, 0, strlen(beseda));
}