Skip to content

Instantly share code, notes, and snippets.

@TobleMiner
Created January 21, 2016 08:54
Show Gist options
  • Select an option

  • Save TobleMiner/73385ab11320ad68e8a3 to your computer and use it in GitHub Desktop.

Select an option

Save TobleMiner/73385ab11320ad68e8a3 to your computer and use it in GitHub Desktop.
MorseCoder with "DEBUG" output
package programming.set9.morses;
import java.nio.charset.Charset;
public class MorseCoder
{
private static boolean DEBUG = false;
/**
* Simple static test of the morse encode/decode functions
*
* @param args
* program arguments
*/
public static void main(String[] args)
{
String hell0W0rld = encode("Thi(s§ ju)mßbl!ed me+#s*#s");
System.out.println(hell0W0rld);
System.out.println(decode(hell0W0rld));
}
/**
* All required Chars offset by 0x30 (ASCII value of 0) with a few filler
* characters to fill the gap between 9 and A
*/
private static final String[] charToMorse = { "-----", ".----", "..---", "...--",
"....-", ".....", "-....", "--...", "---..", "----.", "", "", "", "", "", "",
"", ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-",
"...-", ".--", "-..-", "-.--", "--.." };
/**
* Returns a new string which is the morse code version of the input string.
* Each word is on a separate line. The morse representation of each
* character of the input string is separated from the next by a space
* character in the output string.
*
* @param input
* the input string.
* @return the morse code version of the input string, ignoring all
* characters in the input string that cannot be represented in
* international morse code.
*/
public static String encode(String input)
{
if(DEBUG) throw new RuntimeException("Encode: " + input);
// Wipe all invalid characters off the face of the earth
input = input.replaceAll("[^A-Za-z0-9\n ]", "");
String morse = "";
for(String word : input.split("[ \\n]+"))
{
for(byte c : word.toUpperCase().getBytes(Charset.forName("US-ASCII")))
{
int index = c - '0';
// Check if the morse code table contains our char
if(index >= 0 && index < charToMorse.length &&
charToMorse[index].length() != 0)
{
morse += charToMorse[index] + " ";
}
}
morse = morse.trim() + "\n";
}
return morse;
}
/**
* Returns a new string which is the natural-language version of the input
* string, which is assumed to be in morse code format as produced by the
* encode method.
*
* @param input
* morse code input string.
* @return natural language version or {@code null} if the input string
* could not be properly parsed.
*/
public static String decode(String input)
{
String output = "";
for(String word : input.split("\\n"))
{
for(String morse : word.split(" +"))
{
Character c = morseToChar(morse);
// Break if the morse code contains any invalid sequences
if(c == null) return null;
output += c;
}
output += " ";
}
// Remove any unwanted whitespace characters
return output.trim();
}
/**
* Searches the morse code table for a the given morse code and returns the
* corresponding char or {@code null} if the morse code wasn't found
*
* @param str
* The code to search
* @return The corresponding character or {@code null} if the morse code
* wasn't found
*/
static private Character morseToChar(String str)
{
// Search the lookup table for str
for(int i = 0; i < charToMorse.length; i++)
{
if(charToMorse[i].equals(str)) return (char) ('0' + i);
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment