For this assignment you are to write a program that takes a

For this assignment you are to write a program that takes a string as input, and print out all permutations of the characters in that string. using loops

Solution

package backtracking;

public class StringPermutationWithoutRepetition

{

public static void main(String[] args)

{

permutation(\"ABC\");

}

private static void permutation(String string)

{

printPermutation(string,\"\");

}

private static void printPermutation(String string, String permutation)

{

if(string.length()==0)

{  

System.out.println(permutation);

return;

}

for (int i = 0; i < string.length(); i++)

{

char toAppendToPermutation = string.charAt(i);

String remaining = string.substring(0, i) + string.substring(i + 1);

printPermutation( remaining, permutation + toAppendToPermutation);

}

}

}

Now the output will be like this in the given below:

Case 1

Input:   A

output: A

Case 2

Input: AB

output:

AB

BA

   Case 3

   Input: ABC

   output:

   ABC

   ACB

   BAC

   BCA

   CAB

   CBA

For this assignment you are to write a program that takes a string as input, and print out all permutations of the characters in that string. using loopsSolutio
For this assignment you are to write a program that takes a string as input, and print out all permutations of the characters in that string. using loopsSolutio

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site