0

I need to print the permutation in the order, for example, abc must be (abc, acb, bac, bca, cab, cba), but my program does it differently. It is printing cba before cab, which is not what I wanted; what can I do?

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


void troca(char *x, char *y){
  char temp;
  temp = *x;
  *x = *y;
  *y = temp;
}


void jeitos(char *a, int f, int h){

  int i;

  if (f == h){
    printf("%s\n", a);
  }
  else{
    for (i = f; i <= h; i++){
      troca((a + f), (a + i));
      jeitos(a, f + 1, h);
      troca((a + f), (a + i)); 
    } 
  }
}

int main(){
    
  char string[6];
  scanf("%s", string);
  int n = strlen(string);
  jeitos(string, 0, n-1);
  
  return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

0 Answers0