encode static method

String encode(
  1. String text
)

Implementation

static String encode(String text) {
  // Define the vowel and consonant lists
  List<String> vowels = ['A', 'E', 'I', 'O', 'U'];
  List<String> consonants = [
    'B',
    'C',
    'D',
    'F',
    'G',
    'H',
    'J',
    'K',
    'L',
    'M',
    'N',
    'P',
    'Q',
    'R',
    'S',
    'T',
    'V',
    'W',
    'X',
    'Y',
    'Z'
  ];

  // Create a map for easy lookups
  Map<String, String> vowelMap = {};
  Map<String, String> consonantMap = {};

  // Fill the maps by mirroring the lists
  for (int i = 0; i < vowels.length; i++) {
    vowelMap[vowels[i]] = vowels[vowels.length - 1 - i];
  }

  for (int i = 0; i < consonants.length; i++) {
    consonantMap[consonants[i]] = consonants[consonants.length - 1 - i];
  }

  // Encode the text
  StringBuffer encoded = StringBuffer();
  for (int i = 0; i < text.length; i++) {
    String char = text[i].toUpperCase();
    if (vowelMap.containsKey(char)) {
      encoded.write(vowelMap[char]);
    } else if (consonantMap.containsKey(char)) {
      encoded.write(consonantMap[char]);
    } else {
      encoded.write(char); // Keep non-alphabet characters unchanged
    }
  }
  return encoded.toString();
}