Loading...
Engaged Employer
Write a program that reverses the words in a sentence.
Anonymous
I used a stack, pushing words onto it as I reached spaces.
String[] temp; temp = input.split(" "); System.out.println("Array length : " + temp.length); for (int i = temp.length; i > 0; i--) System.out.print(temp[i-1] + " " ); System.out.println(""); }
public static String reverseSentence(String a){ StringBuilder result = new StringBuilder(); Stack stack = new Stack(); String[] temp; temp = a.split(" "); for (String s : temp){ stack.push(s); } while(!stack.empty()){ result.append(stack.pop() + " "); } return result.toString(); }
=0; $index--) { $result[strlen($word)-1 - $index] = $word[$index]; } return implode("", $result); } // O(n/2) times = O(n) times function reverse2($word) { $temp = ""; if(strlen($word)%2 == 1) $stop = strlen($word)/2; else $stop = strlen($word)/2-1; for ($index = 0; $index
Check out your Company Bowl for anonymous work chats.
Get actionable career advice tailored to you by joining more bowls.
Stay ahead in opportunities and insider tips by following your dream companies.
Get personalized job recommendations and updates by starting your searches.