Advertisement
FedchenkoIhor

reversedword task

Oct 18th, 2021
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. public class ReversWords {
  2.  
  3.     public static String reversed(String input) {
  4.         String process = input;
  5.         String result = "";
  6.         while (process.length() > 0) {
  7.             // условие когда осталось первое (оно же в результате последнее) слово и не осталось пробелов
  8.             if (!process.contains(" ")) {
  9.                 result = result.concat(process);
  10.                 return result;
  11.             }
  12.             // находим последний слово и добавляем в результат
  13.             result = result.concat(process.substring(process.lastIndexOf(" ") + 1) + " ");
  14.             // удаляем последнее слово
  15.             process = process.substring(0, process.lastIndexOf(" "));
  16.         }
  17.         return result;
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement