fork(2) download
  1. import java.util.Scanner;
  2.  
  3. public class Main
  4. {
  5. public static void main(String[] args)
  6. {
  7. Scanner sc = new Scanner(System.in);
  8. System.out.println("Enter the string:");
  9. String s = sc.nextLine();
  10. char[] c = s.toCharArray();
  11.  
  12. System.out.println("Here are all the permutations.");
  13. permutation(c, 0);
  14. }
  15.  
  16. public static void permutation(char[] c, int start)
  17. {
  18. if (start == c.length) {
  19. System.out.println(c);
  20. }
  21.  
  22. for (int i = start; i < c.length; i++) {
  23. char temp = c[start];
  24. c[start] = c[i];
  25. c[i] = temp;
  26. permutation(c, start+1);
  27. temp = c[start];
  28. c[start] = c[i];
  29. c[i] = temp;
  30. }
  31. }
  32. }
Success #stdin #stdout 0.1s 380608KB
stdin
john
stdout
Enter the string:
Here are all the permutations.
john
jonh
jhon
jhno
jnho
jnoh
ojhn
ojnh
ohjn
ohnj
onhj
onjh
hojn
honj
hjon
hjno
hnjo
hnoj
nohj
nojh
nhoj
nhjo
njho
njoh