Enhance docs, add more tests in PhoneticAlphabetConverter (#5943)

This commit is contained in:
Hardik Pawar
2024-10-23 12:11:48 +05:30
committed by GitHub
parent 757d10c277
commit 03fe106e32
2 changed files with 34 additions and 12 deletions

View File

@ -58,9 +58,25 @@ public final class PhoneticAlphabetConverter {
PHONETIC_MAP.put('9', "Nine");
}
/**
* Converts text to the NATO phonetic alphabet.
* Steps:
* 1. Convert the text to uppercase.
* 2. Iterate over each character in the text.
* 3. Get the phonetic equivalent of the character from the map.
* 4. Append the phonetic equivalent to the result.
* 5. Append a space to separate the phonetic equivalents.
* 6. Return the result.
*
* @param text the text to convert
* @return the NATO phonetic alphabet
*/
public static String textToPhonetic(String text) {
StringBuilder phonetic = new StringBuilder();
for (char c : text.toUpperCase().toCharArray()) {
if (Character.isWhitespace(c)) {
continue;
}
phonetic.append(PHONETIC_MAP.getOrDefault(c, String.valueOf(c))).append(" ");
}
return phonetic.toString().trim();