1. /**
  2. * @author Jose M Vidal <jmvidal@gmail.com>
  3. *
  4. * Twitter to Leet translator.
  5. */
  6. 
  7. import java.util.Scanner;
  8. 
  9. public class LeetTranslator {
  10. 
  11. /**
  12. * @param args ignored
  13. */
  14. public static void main(String[] args) {
  15. Scanner keyboard = new Scanner(System.in);
  16. System.out.println("I will translate your tweet to Leet.");
  17. System.out.print("Enter your tweet:");
  18. String message = keyboard.nextLine();
  19. message = message.toLowerCase();
  20. message = message + ' '; //add space at the end, in case it was not there
  21. 
  22. //pick out the hashtag if there
  23. int hashIndex = message.indexOf('#');
  24. int hashEndIndex = -1;
  25. String hashTag = "";
  26. if (hashIndex != -1) { // there is a # mark
  27. hashEndIndex = message.indexOf(' ', hashIndex);
  28. hashTag = message.substring(hashIndex, hashEndIndex);
  29. if (hashEndIndex == hashIndex + 1) { //oops, it is the empty #
  30. System.out.println("No Hashtag found.");
  31. }
  32. else {
  33. System.out.println("Hashtag: " + hashTag);
  34. }
  35. } else {
  36. System.out.println("No Hashtag found.");
  37. };
  38. 
  39. //Make leet replacements. This will mess up the haschode, but we will fix that later.
  40. message = message.replaceAll("and", "&");
  41. message = message.replaceAll("a", "4");
  42. message = message.replaceAll("b", "8");
  43. message = message.replaceAll("e", "3");
  44. message = message.replaceAll("g", "6");
  45. message = message.replaceAll("l", "1");
  46. message = message.replaceAll("o", "0");
  47. message = message.replaceAll("s", "5");
  48. message = message.replaceAll("t", "7");
  49. message = message.replaceAll("z", "2");
  50. 
  51. //If there was a hashcode, put it back in.
  52. if (hashIndex != -1) {
  53. String leftPart = message.substring(0,hashIndex);
  54. String rightPart = message.substring(hashEndIndex);
  55. message = leftPart + hashTag + rightPart;
  56. };
  57. 
  58. System.out.println("Translation: " + message);
  59. System.out.println("Message has " + message.length() + " characters.");
  60. if (message.length() > 140){
  61. System.out.println("ERROR: Too big to tweet.");
  62. };
  63. }
  64. }