Advertisement
dave_kamenenko

6 laba

Nov 16th, 2021
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. package com.company;
  2.  
  3. class Main {
  4.  
  5.     static String maxWord = "";
  6.  
  7.     static void MaxLengthWords(String input) {
  8.         int len = input.length();
  9.         int si = 0, ei = 0;
  10.         int max_length = 0, max_start_index = 0;
  11.  
  12.         // Loop while input string is not empty
  13.         while (ei <= len) {
  14.             if (ei < len && input.charAt(ei) != ' ') {
  15.                 ei++;
  16.             } else {
  17.                 // end of a word
  18.                 // find curr word length
  19.                 int curr_length = ei - si;
  20.  
  21.                 if (curr_length > max_length) {
  22.                     max_length = curr_length;
  23.                     max_start_index = si;
  24.                 }
  25.                 ei++;
  26.                 si = ei;
  27.             }
  28.         }
  29.         maxWord = input.substring(max_start_index, max_length);
  30.     }
  31.  
  32.     // Driver code
  33.     public static void main(String[] args) {
  34.         String a = "Example of a search string";
  35.  
  36.         MaxLengthWords(a);
  37.         System.out.print("\nMaximum length word: "
  38.                 + maxWord);
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement