Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SwapWords {
- public static void main(String[] args) {
- String text = "London is the capital of Great Britain";
- String[] words = text.split(" ");
- String maxLength = Arrays.stream(words).max(Comparator.comparing(String::length)).get();
- String minLength = Arrays.stream(words).min(Comparator.comparing(String::length)).get();
- int idxOfMax = getWordIndex(words, maxLength);
- int idxOfMin = getWordIndex(words, minLength);
- words[idxOfMax] = minLength;
- words[idxOfMin] = maxLength;
- System.out.println(Strings.join(words, " "));
- }
- public static int getWordIndex(String[] array, String word) {
- int idx = 0;
- for (int i = 0; i < array.length; i++) {
- if (array[i].equals(word)) {
- idx = i;
- }
- }
- return idx;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement