Anagram Generator
From Java Example Source Code
Contents |
[edit] Overview - Anagram Generator
This is a Java example program.
[edit] Java Source Code
- Package: example.algorithms
- File: AnagramApp.java
package example.algorithms;
import java.io.IOException;
public class AnagramApp {
static int size;
static int count;
static char[] charArray;
public static void main(String[] args) throws IOException {
String input = "Java Source and Support";
size = input.length();
count = 0;
charArray = new char[size];
for (int j = 0; j < size; j++)
charArray[j] = input.charAt(j);
doAnagram(size);
}public static void doAnagram(int newSize) {
int limit;if (newSize == 1) // if too small, return;
return;// for each position,for (int i = 0; i < newSize; i++) {
doAnagram(newSize - 1); // anagram remaining
if (newSize == 2) // if innermost,
display();
rotate(newSize); // rotate word
}}// rotate left all chars from position to endpublic static void rotate(int newSize) {
int i;int position = size - newSize;
// save first letterchar temp = charArray[position];
// shift others leftfor (i = position + 1; i < size; i++)
charArray[i - 1] = charArray[i];
// put first on rightcharArray[i - 1] = temp;
}public static void display() {
System.out.print(++count + " ");
for (int i = 0; i < size; i++)
System.out.print(charArray[i]);
System.out.println();
}}
[edit] What Result You Can Get
Run the program, you will get:
6547087 Java Source ntaupor Spd 6547088 Java Source ntaupor Sdp 6547089 Java Source ntaupor pdS 6547090 Java Source ntaupor pSd 6547091 Java Source ntaupor dSp 6547092 Java Source ntaupor dpS 6547093 Java Source ntauporSpd 6547094 Java Source ntauporSp d 6547095 Java Source ntauporSd p 6547096 Java Source ntauporSdp 6547097 Java Source ntauporS pd ...
[edit] Required External Libraries and/or Files for this Java Example
Need nothing.
[edit] How to Run this Java Example Program
We recommend running this Java example program with Eclipse.
For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.
It's fairly easy.
[edit] Question & Answer
Any question?
Click edit and post your question or answer here.
