Search Tutorials

Loading...

Tuesday, 23 April 2013

Java code to print possible Permutations of a String

Java Program to print the possible Permutations of a String.

import java.io.*;
class Combination
{
 public static void main(String args[]) throws IOException
 {
 Combination call = new Combination();
 BufferedReader br = new BufferedReader(new
 InputStreamReader(System.in));
 System.out.print("Enter a String : ");
 String n = br.readLine();
 call.comb("",n);
 }
 public void comb(String beg, String end) throws IOException
 {
 if (end.length()<=1)
 {
  System.out.println(beg+end);
 }
 else
 {
  for (int i=0;i<end.length();i++)
  {
   String n = end.substring(0,i)+end.substring(i+1);
   comb(beg + end.charAt(i),n);
  }
 }
 }
}

ALGORITHM:-

1. Start

2. Accept a string from the user.

3. Using recursion find all the combinations of the string.

4. Print the combinations.

5. End

OUTPUT:-

Java code to print possible Permutations of a String

Enter a String : TOP
TOP
TPO
OTP
OPT
PTO
POT

Related Programs:-

Convert Decimal to Octal

Convert Decimal to Binary

Print the Factorial of a number using recursion

Print the possible combination of a string

Generate Fibonacci series using recursion

2 comments:

  1. I think this is called Permutations not combinations because in combintation order does not matter.
    refer - http://www.mathsisfun.com/combinatorics/combinations-permutations.html

    Combination for TOP would be T, TO, TOP, TP, OP, OP, P

    ReplyDelete

Back to Top