.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: Consolas, "Courier New", Courier, Monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #a31515; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
import java.io.*;
public class Anagrams
{
public static void main(String[] args)throws IOException
{
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a string:");
String s = r.readLine();
char[] text = new char[s.length()];
for (int i=0; i<s.length(); i++)
{
text[i] = s.charAt(i);
}
System.out.println("Here are all the anagrams of " + s);
printAnagrams(text, 0);
}
public static void printAnagrams(char[] a, int i)
{
if (i == a.length-1) printArray(a);
else {
for (int j=i; j< a.length; j++) {
//swap a[i] with a[j]
char c = a[i];
a[i] = a[j];
a[j] = c;
printAnagrams(a, i+1);
//swap back
c = a[i];
a[i] = a[j];
a[j] = c;
}
}
}
static void printArray(char [] a)
{
for (int i=0; i< a.length; i++) System.out.print(a[i]);
System.out.println();
}
}