Microsoft Interview Question

Reverse the words in a String but not the letters themselves. For example, "Hello World" -> "World Hello".

Interview Answers

Anonymous

Nov 17, 2016

In Java: String reverseWords(String s){ String[] parts = s.split(" "); // split the string to words String temps; int begin = 0; int end = parts.length -1; while(begin < end){ // reverse the string temp = parts[begin]; parts[begin] = parts[end]; parts[end] = temp; } StringBuilder strBuilder = new StringBuilder(); // rebuild the string for (int i = 0; i < parts.length; i++) { strBuilder.append(parts[i]); } return strBuilder.toString(); }

Anonymous

Jun 19, 2020

String reverseWords(“Hello World”)