Skip to content

Instantly share code, notes, and snippets.

@dimitar-mihov17
Created October 3, 2021 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimitar-mihov17/7bd2ff2c9bcd4e3e39fa38445c607ff0 to your computer and use it in GitHub Desktop.
Save dimitar-mihov17/7bd2ff2c9bcd4e3e39fa38445c607ff0 to your computer and use it in GitHub Desktop.
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