<pre>
import java.io.*;

public class per{
    per(String ipStr)throws Exception{//constructor
        start("",ipStr);
    }
    //-----------------------------
    String reverseString(String str)throws Exception{//reverse the string
        String toReturn="";
        for(int i=str.length()-1;i>=0;i--){
           toReturn+=str.substring(i,i+1);
        }return toReturn;
    }
    //-----------------------------
    String swapTwoCharacter(String str,int index)throws Exception{//swap two character
        String toReturn=null;
        if(index<str.length()&&index>0){
            if(str.length()>2){
                char ch_ind = str.charAt(index);//inbetween character
                char ch_start = str.charAt(0);//starting char

                String middle=str.substring(1,index);
                String tail=str.substring(1+index);

                String indexString= new String(new char[]{ch_ind});
                String startString= new String(new char[]{ch_start});

                toReturn=indexString+middle+startString+tail;
            }else if(index<str.length()){//length is 2
               toReturn=reverseString(str);
            }
        }else if(index==0) toReturn=str;
        return toReturn;            
    }
    //-----------------------------
    void start(String processedStr, String remainder)throws Exception{
       String head, tail, newRem;
       if(remainder.length()>3){
           for(int i=0;i<remainder.length();i++){
              if(i!=0) newRem=swapTwoCharacter(remainder,i);
              else newRem=remainder;

              head=newRem.substring(0,1);
              tail=newRem.substring(1);
              start(processedStr+head,tail);//recursive call
           }
       }else{//base-case
           if(remainder.length()==1)
              System.out.println("="+remainder+"=");
           else if(remainder.length()==2){
              System.out.println("="+remainder+"=");
              System.out.println("="+reverseString(remainder)+"=");
           }else{
               printData(processedStr, remainder, 0);//each call
               printData(processedStr, remainder, 1);//prints two combinations
               printData(processedStr, remainder, 2);
           }
       }
    }
    //-----------------------------
    void printData(String processedStr, String remainder, int index)throws Exception{
          //print data in given base-case input
          String head, tail, newRem;
          newRem=swapTwoCharacter(remainder,index);
          head=newRem.substring(0,1);
          tail=newRem.substring(1);
          System.out.println("="+processedStr+newRem+"=");
          System.out.println("="+processedStr+head+reverseString(tail)+"=");
    }
    //-----------------------------
    public static void main(String args[])throws Exception{
        try{new per(args[0]);}//accept input
        catch(Exception e){System.out.println("error 934"+e);}
    }
}
</pre>
