Programming and general geekiness.

Posts tagged ‘english’

Java anagram code

After having coded a nice simple Java app to help to solve Windows 8′s Treehouse Stampede anagrams I thought I could adapt it so that it will show the five longest words that can be formed from a phrase of any given link. Here’s the code in full:

import java.io.*;
class anagram
{
	public static void main (String[] args)
	{
		//Settings
		String[] array = new String[60000];
		int next = 0;
		try
		{
			FileInputStream fstream = new FileInputStream("englishwords.txt");
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String line;
			while ((line = br.readLine()) != null)
			{
				array[next] = line;
				next++;
			}
		}
		catch (Exception e)
		{
			System.err.println("Error: " + e.getMessage());
		}
		System.out.println("OK, loaded up the file");

		while (true)
		{
			anagram cd = new anagram();
			Console console = System.console();
			String input = console.readLine("Letters: ");
			if (input.equals("exit")) break;
			if (input.length() > 0)
			{
				String[] top = new String[5];
				top[0] = "";
				top[1] = "";
				top[2] = "";
				top[3] = "";
				top[4] = "";
				for(String word : array)
				{
					if (word != null)
					{
						boolean good = true;
						for (int y = 0; y < input.length(); y++)
						{
							char cA = input.charAt(y);
							if (cd.occur(word, cA) > cd.occur(input, cA))
							{
								good = false;
								break;
							}
						}
						for (int y = 0; y < word.length(); y++)
						{
							char cA = word.charAt(y);
							if (cd.occur(input, cA) == 0) //Input has the letter
							{
								good = false;
								break;
							}
						}
						if (good)
						{
							if (word.length() > top[0].length())
							{
								top[4] = top[3];
								top[3] = top[2];
								top[2] = top[1];
								top[1] = top[0];
								top[0] = word;
							}
							else if (word.length() > top[1].length())
							{
								top[4] = top[3];
								top[3] = top[2];
								top[2] = top[1];
								top[1] = word;
							}
							else if (word.length() > top[2].length())
							{
								top[4] = top[3];
								top[3] = top[2];
								top[2] = word;
							}
							else if (word.length() > top[3].length())
							{
								top[4] = top[3];
								top[3] = word;
							}
							else if (word.length() > top[4].length())
							{
								top[4] = word;
							}
						}
					}
				}
				System.out.println("1: " + top[0]);
				System.out.println("2: " + top[1]);
				System.out.println("3: " + top[2]);
				System.out.println("4: " + top[3]);
				System.out.println("5: " + top[4]);
			}
		}
	}

	public int occur(String haystack, char needle)
	{
		int count = 0;
		for (int i = 0; i < haystack.length(); i++)
		{
			if (haystack.charAt(i) == needle) count++;
		}
		return count;
	}
}

Here are the instructions:

  1. Copy the code into a blank text file and save it as anagram.java
  2. Download my englishwords.txt file which contains over 60,000 English words and copy it into the same folder
  3. Compile the code with javac anagram.java
  4. Run java anagram
  5. Type in your phrase to get the top five anagrams
  6. Type exit to quit the program
Here’s a list of my favorite results:
  • David Cameron: America/Comedian
  • Microsoft Windows: Discomfort
  • Programming Thomas: Pragmatism
  • Google Mail: Amigo
  • Mozilla Firefox: Floozier/foamier
  • Wikipedia: Pawed
  • Barack Hussein Obama: Chainsmoke
  • United States of America: Dramatisation (using English-UK spelling)
  • Bill Gates: Ballet
  • New York Times: Enormity

Sports invented in England vs. English Success

Follow

Get every new post delivered to your Inbox.