Skip to content

Commit e9b897b

Browse files
authored
Add PhoneticAlphabetConverter (#5752)
1 parent 5a710ea commit e9b897b

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
* [OctalToBinary](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToBinary.java)
101101
* [OctalToDecimal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToDecimal.java)
102102
* [OctalToHexadecimal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/OctalToHexadecimal.java)
103+
* [PhoneticAlphabetConverter](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/PhoneticAlphabetConverter.java)
103104
* [RgbHsvConversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/RgbHsvConversion.java)
104105
* [RomanToInteger](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/RomanToInteger.java)
105106
* [TurkishToLatinConversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/conversions/TurkishToLatinConversion.java)
@@ -740,6 +741,7 @@
740741
* [OctalToBinaryTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToBinaryTest.java)
741742
* [OctalToDecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToDecimalTest.java)
742743
* [OctalToHexadecimalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/OctalToHexadecimalTest.java)
744+
* [PhoneticAlphabetConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/PhoneticAlphabetConverterTest.java)
743745
* [RomanToIntegerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/RomanToIntegerTest.java)
744746
* [UnitConversionsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/UnitConversionsTest.java)
745747
* [UnitsConverterTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/conversions/UnitsConverterTest.java)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.thealgorithms.conversions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Converts text to the NATO phonetic alphabet.
8+
* Examples:
9+
* "ABC" -> "Alpha Bravo Charlie"
10+
* "Hello" -> "Hotel Echo Lima Lima Oscar"
11+
* "123" -> "One Two Three"
12+
* "A1B2C3" -> "Alpha One Bravo Two Charlie Three"
13+
*
14+
* @author Hardvan
15+
*/
16+
public final class PhoneticAlphabetConverter {
17+
private PhoneticAlphabetConverter() {
18+
}
19+
20+
private static final Map<Character, String> PHONETIC_MAP = new HashMap<>();
21+
22+
static {
23+
PHONETIC_MAP.put('A', "Alpha");
24+
PHONETIC_MAP.put('B', "Bravo");
25+
PHONETIC_MAP.put('C', "Charlie");
26+
PHONETIC_MAP.put('D', "Delta");
27+
PHONETIC_MAP.put('E', "Echo");
28+
PHONETIC_MAP.put('F', "Foxtrot");
29+
PHONETIC_MAP.put('G', "Golf");
30+
PHONETIC_MAP.put('H', "Hotel");
31+
PHONETIC_MAP.put('I', "India");
32+
PHONETIC_MAP.put('J', "Juliett");
33+
PHONETIC_MAP.put('K', "Kilo");
34+
PHONETIC_MAP.put('L', "Lima");
35+
PHONETIC_MAP.put('M', "Mike");
36+
PHONETIC_MAP.put('N', "November");
37+
PHONETIC_MAP.put('O', "Oscar");
38+
PHONETIC_MAP.put('P', "Papa");
39+
PHONETIC_MAP.put('Q', "Quebec");
40+
PHONETIC_MAP.put('R', "Romeo");
41+
PHONETIC_MAP.put('S', "Sierra");
42+
PHONETIC_MAP.put('T', "Tango");
43+
PHONETIC_MAP.put('U', "Uniform");
44+
PHONETIC_MAP.put('V', "Victor");
45+
PHONETIC_MAP.put('W', "Whiskey");
46+
PHONETIC_MAP.put('X', "X-ray");
47+
PHONETIC_MAP.put('Y', "Yankee");
48+
PHONETIC_MAP.put('Z', "Zulu");
49+
PHONETIC_MAP.put('0', "Zero");
50+
PHONETIC_MAP.put('1', "One");
51+
PHONETIC_MAP.put('2', "Two");
52+
PHONETIC_MAP.put('3', "Three");
53+
PHONETIC_MAP.put('4', "Four");
54+
PHONETIC_MAP.put('5', "Five");
55+
PHONETIC_MAP.put('6', "Six");
56+
PHONETIC_MAP.put('7', "Seven");
57+
PHONETIC_MAP.put('8', "Eight");
58+
PHONETIC_MAP.put('9', "Nine");
59+
}
60+
61+
public static String textToPhonetic(String text) {
62+
StringBuilder phonetic = new StringBuilder();
63+
for (char c : text.toUpperCase().toCharArray()) {
64+
phonetic.append(PHONETIC_MAP.getOrDefault(c, String.valueOf(c))).append(" ");
65+
}
66+
return phonetic.toString().trim();
67+
}
68+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class PhoneticAlphabetConverterTest {
8+
9+
@Test
10+
public void testTextToPhonetic() {
11+
assertEquals("Alpha Bravo", PhoneticAlphabetConverter.textToPhonetic("AB"));
12+
assertEquals("Alpha Bravo Charlie", PhoneticAlphabetConverter.textToPhonetic("ABC"));
13+
assertEquals("Alpha One Bravo Two Charlie Three", PhoneticAlphabetConverter.textToPhonetic("A1B2C3"));
14+
assertEquals("Hotel Echo Lima Lima Oscar", PhoneticAlphabetConverter.textToPhonetic("Hello"));
15+
assertEquals("One Two Three", PhoneticAlphabetConverter.textToPhonetic("123"));
16+
assertEquals("Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine",
17+
PhoneticAlphabetConverter.textToPhonetic("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"));
18+
assertEquals("Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu Zero One Two Three Four Five Six Seven Eight Nine",
19+
PhoneticAlphabetConverter.textToPhonetic("abcdefghijklmnopqrstuvwxyz0123456789"));
20+
}
21+
}

0 commit comments

Comments
 (0)