C++ Write a simple \"translator\" program using a map where the keys are English
ID: 3535708 • Letter: C
Question
C++
Write a simple "translator" program using a map where the keys are English words and the values are corresponding words in another language. (Pick a language you know, if possible. Otherwise, pick whatever you want and look up some vocabulary for it.) Include at least 10 words. The way it should work is: Ask the user to enter a complete sentence. Break the sentence into words. For each word, figure out if there is a translation available in your map. If there is, replace the word with its translation. If not, leave the original English word alone, Print out the final translated version of the sentence.Explanation / Answer
import java.util.Scanner;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EnglishToLeetConvertor {
private HashMap<Character, String> map = new HashMap<Character, String>();
public EnglishToLeetConvertor() {
map.put('A', "@");
map.put('B', "]3");
map.put('C', "(");
map.put('D', "|)");
map.put('E', "€");
map.put('F', "|=");
map.put('G', "(_>");
map.put('H', "|-|");
map.put('I', "!");
map.put('J', "_|");
map.put('K', "|<");
map.put('L', "|_");
map.put('M', "|V|");
map.put('N', "|V");
map.put('O', "0");
map.put('P', "|>");
map.put('Q', "0_");
map.put('R', "|2");
map.put('S', "$");
map.put('T', "7");
map.put('U', "|_|");
map.put('V', "\/");
map.put('W', "uJ");
map.put('X', "><");
map.put('Y', " ¥");
map.put('Z', "z");
}
public String toLeetCode(String str){
Pattern pattern = Pattern.compile( "[^a-zA-Z]" );
StringBuilder result=new StringBuilder();
for(int i=0;i<str.length();i++){
char key=Character.toUpperCase(str.charAt(i));
Matcher matcher = pattern.matcher(Character.toString(key));
if(matcher.find()){
result.append(key);
result.append(' ');
}
else{
result.append(map.get(key));
result.append(' ');
}
}
return result.toString();
//String[] retval=str.split(" ");
}
public static void main(String[] args){
EnglishToLeetConvertor ob=new EnglishToLeetConvertor();
Scanner scanner=new Scanner(System.in);
System.out.println("Input English Words :- ");
String engWord=scanner.nextLine();
String leet=ob.toLeetCode(engWord);
System.out.println("The 1337 Code is :- "+leet);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.