Amazon interview question

Write a program to reverse words in a string

Interview Answers

Anonymous

29 Jan 2011

The above answer isn't correct, because it would reverse the order of the letters as well. For example: The quick brown fox => xof nworb kciuq ehT when it should be: fox brown quick The. You have to make another loop inside to traverse the length of each word, and then print that out in the correct order.

1

Anonymous

9 Mar 2011

in java, you can use string split. public class TestClass { public static void main(String[] args){ String str = "The quick fox jump over the dog"; String[] strs = str.split(str, ' '); String result = ""; for (int i = strs.length-1; i > 0; i--){ result += strs[i]; } System.out.println(result); } }

Anonymous

9 Mar 2011

public class TestClass{ public static void main(String[] args){ String str = "The quick fox jump over the dog"; String[] strs = str.split(" "); System.out.println("the output is: "); String result = ""; for (int i = strs.length-1; i >= 0; i--){ result = (result.length()>0?(result+" "):result)+strs[i]; } System.out.println(result); } }

Anonymous

29 Jan 2011

#include int main(int argc, char **argv) { int i; for(i = argc-1; i > 0 ; i--) { printf ("%s ", argv[i]); } printf ("\n"); return 0; }