Tuesday, April 23, 2024
HomeSoftware EngineeringConvert String to LeetSpeak in Java

Convert String to LeetSpeak in Java


The problem

Your process is to jot down a perform toLeetSpeak that converts an everyday english sentence to Leetspeak.

Extra about LeetSpeak You possibly can learn at wiki -> https://en.wikipedia.org/wiki/Leet

Contemplate solely uppercase letters (no lowercase letters, no numbers) and areas.

For instance:

toLeetSpeak("LEET") returns "1337"

On this problem we use a easy LeetSpeak dialect. Use this alphabet:

{
  A : '@',
  B : '8',
  C : '(',
  D : 'D',
  E : '3',
  F : 'F',
  G : '6',
  H : '#',
  I : '!',
  J : 'J',
  Okay : 'Okay',
  L : '1',
  M : 'M',
  N : 'N',
  O : '0',
  P : 'P',
  Q : 'Q',
  R : 'R',
  S : '$',
  T : '7',
  U : 'U',
  V : 'V',
  W : 'W',
  X : 'X',
  Y : 'Y',
  Z : '2'
}

The answer in Java code

Possibility 1:

import java.util.*;
class Answer {
    static String toLeetSpeak(closing String converse) {
        StringBuilder sb = new StringBuilder();
        Map<Character, Character> m = new HashMap<>()
        {{
          put('A', '@');
          put('B', '8');
          put('C', '(');
          put('D', 'D');
          put('E', '3');
          put('F', 'F');
          put('G', '6');
          put('H', '#');
          put('I', '!');
          put('J', 'J');
          put('Okay', 'Okay');
          put('L', '1');
          put('M', 'M');
          put('N', 'N');
          put('O', '0');
          put('P', 'P');
          put('Q', 'Q');
          put('R', 'R');
          put('S', '$');
          put('T', '7');
          put('U', 'U');
          put('V', 'V');
          put('W', 'W');
          put('X', 'X');
          put('Y', 'Y');
          put('Z', '2');
        }};
        for (Character c : converse.toCharArray()) {
            Character a = m.getOrDefault(c, c);
            sb.append(a);
        }
        return sb.toString();
    }
}

Possibility 2:

class Answer {
    static String toLeetSpeak(closing String converse) {
        StringBuilder sb = new StringBuilder(converse.size());
        for (char c: converse.toCharArray()) {
          swap (c) {
            case 'A': sb.append('@'); break;
            case 'B': sb.append('8'); break;
            case 'C': sb.append('('); break;
            case 'E': sb.append('3'); break;
            case 'G': sb.append('6'); break;
            case 'H': sb.append('#'); break;
            case 'I': sb.append('!'); break;
            case 'L': sb.append('1'); break;
            case 'O': sb.append('0'); break;
            case 'S': sb.append('$'); break;
            case 'T': sb.append('7'); break;
            case 'Z': sb.append('2'); break;
            default: sb.append(c); break;
          }
        }
        return sb.toString();
    }
}

Possibility 3:

class Answer {
  personal static closing String LEET = "@8(D3F6#!JK1MN0PQR$7UVWXY2";
  static String toLeetSpeak(closing String converse) {
    String leet = "";
    for (closing char c : converse.toCharArray())
        leet += c>='A' && c<='Z' ? LEET.charAt(c-'A') : c;
    return leet;
  }
}

Check circumstances to validate our answer

import org.junit.Check;
import static org.junit.Assert.assertEquals;

public class SolutionTest {
    @Check public void testSomething() {
        assertEquals("1337", Answer.toLeetSpeak("LEET"));
        assertEquals("#3110 W0R1D", Answer.toLeetSpeak("HELLO WORLD"));
        assertEquals("10R3M !P$UM D010R $!7 @M37", Answer.toLeetSpeak("LOREM IPSUM DOLOR SIT AMET"));
        assertEquals("7#3 QU!(Okay 8R0WN F0X JUMP$ 0V3R 7#3 [email protected] D06", Answer.toLeetSpeak("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"));
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments