Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- class Main {
- static String maxWord = "";
- static void MaxLengthWords(String input) {
- int len = input.length();
- int si = 0, ei = 0;
- int max_length = 0, max_start_index = 0;
- // Loop while input string is not empty
- while (ei <= len) {
- if (ei < len && input.charAt(ei) != ' ') {
- ei++;
- } else {
- // end of a word
- // find curr word length
- int curr_length = ei - si;
- if (curr_length > max_length) {
- max_length = curr_length;
- max_start_index = si;
- }
- ei++;
- si = ei;
- }
- }
- maxWord = input.substring(max_start_index, max_length);
- }
- // Driver code
- public static void main(String[] args) {
- String a = "Example of a search string";
- MaxLengthWords(a);
- System.out.print("\nMaximum length word: "
- + maxWord);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement