Problem statement: We need to reverse the sentence containing Japanese and Chinese character.
Ex "ABCabcDEFdef . Here we need to assume that the capital letters are Japanese and smaller letters are Chinese.
We need to get an o/p as "defDEFabcABC"
Steps involved
- Write function to identify if the character is Chinese or Japanese character
- Check if previous character is different than the current. If it is different then add a space
- Use split and then reverse the string
- Remove the empty space
public class ArrayFunctions { /* * * Function to reverse a phrase */ public static String ReversePhrase(String phrase) { String [] words =phrase.split(" "); String rev=""; //loop for(int i=words.length-1;i>=0;i--) { rev=rev+words[i]+ " "; } return rev; } /* * * Function to reverse a word */ public static String ReverseWord(String word) { char [] chars =word.toCharArray(); String rev=""; //loop for(int i=chars.length-1;i>=0;i--) { rev=rev+chars[i]+ " "; } return rev; } /* * Function to reverse string containing japanese or chinese characters */ public static String ReversePhraseChinJap(String phrase) { char [] words =phrase.toCharArray(); String revString=""; //loop for(int i=words.length-1;i>=0;i--) { if(IsChinese(words[i])) { //Check last string in Japanese if(!revString.isEmpty()) { char [] checkJapanese=revString.toCharArray(); char lastChar=checkJapanese[checkJapanese.length-1]; if (IsJapanese(lastChar)) { revString=revString+" "; } } revString=revString+words[i]; } else if(IsJapanese(words[i])) { //Check last string in chinese if(!revString.isEmpty()) { char [] checkChinese=revString.toCharArray(); char lastChar=checkChinese[checkChinese.length-1]; if (IsChinese(lastChar)) { revString=revString+" "; } } revString=revString+words[i]; } } //Reverse of all the characters String [] sent =revString.split(" "); String rev=""; //loop to reverse individual char for(int i=0;i<=sent.length-1;i++) { rev=rev+ ReverseWord(sent[i]); } return rev.replace(" ",""); } /* * Function to check if the given character is chinese */ public static Boolean IsChinese(char chinese) { String chineseCharac ="abcdef"; int exists=chineseCharac.indexOf(chinese); if(exists==-1) return false ; else return true; } /* * Function to check if the given character is japaneese */ public static Boolean IsJapanese(char japanese) { String chineseCharac ="ABCDEF"; int exists=chineseCharac.indexOf(japanese); if(exists==-1) return false ; else return true; } } ;
No comments:
Post a Comment