fork download
  1. public class Solution {
  2. public static String reverseWordWise(String input) {
  3.  
  4. int len = input.length();
  5.  
  6. int i = len-1;
  7. int j = len-1;
  8.  
  9. StringBuilder ans = new StringBuilder();
  10.  
  11.  
  12. while (i >= 0) {
  13.  
  14.  
  15. while (i >= 0 && input.charAt(i) == ' ')
  16. i--;
  17.  
  18.  
  19. j = i;
  20.  
  21.  
  22. while (i >= 0 && input.charAt(i) != ' ')
  23. i--;
  24.  
  25.  
  26. String word = input.substring(i+1, j+1);
  27.  
  28. if (ans.length() == 0) {
  29.  
  30. ans.append(word);
  31. }
  32.  
  33. else {
  34.  
  35. ans.append(" " + word);
  36. }
  37.  
  38. }
  39.  
  40. return ans.toString();
  41. }
  42. }
  43.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:1: error: class Solution is public, should be declared in a file named Solution.java
public class Solution {
       ^
1 error
stdout
Standard output is empty