Posts

Showing posts from August 20, 2018

Reversing the words order of a string

Image
Clash Royale CLAN TAG #URR8PPP .everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0; up vote 1 down vote favorite A couple of weeks I had a coding interview and the problem to solve was very simple: reverse the words order of a string. What did I do wrong here? public static String reverse(String phrase) String words = phrase.split(" "); StringBuffer sb = new StringBuffer(); for (String word : words) String reversed = new String(); for (int j = word.length() - 1; j >= 0; j--) reversed += word.charAt(j); sb = sb.append(reversed).append(" "); return sb.toString().substring(0, sb.toString().length() - 1); java strings interview-questions share | improve this question edited Jan 3 at 23:19 Jamal ♦ 30.1k 11 114 225 asked Jan 3 at 22:27 Eliú Abisaí Díaz Vásquez 8 2 String reversed = new String(); this doesn't look good. Why are you doing this when