Created
October 3, 2021 14:00
-
-
Save dimitar-mihov17/7bd2ff2c9bcd4e3e39fa38445c607ff0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class LongestWordLength | |
{ | |
static int LongestWordLength(String str) | |
{ | |
int n = str.length(); | |
int res = 0, curr_len = 0; | |
for (int i = 0; i < n; i++) | |
{ | |
// If current character is not | |
// end of current word. | |
if (str.charAt(i) != ' ') | |
curr_len++; | |
// If end of word is found | |
else | |
{ | |
res = Math.max(res, curr_len); | |
curr_len = 0; | |
} | |
} | |
// We do max one more time to consider | |
// last word as there won't be any space | |
// after last word. | |
return Math.max(res, curr_len); | |
} | |
public static void main(String[] args) | |
{ | |
String s = "My name is Dimitar Mihov"; | |
System.out.println(LongestWordLength(s)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment