public class ShortestWord {
// Returns the length of the shortest word in the string
public static int shortestWordLength(String text) {
if (text == null || text.isEmpty()) {
return 0;
}
// Split on whitespace (spaces, tabs, newlines)
String[] words = text.trim().split("\\s+");
int minLen = Integer.MAX_VALUE;
for (String word : words) {
int len = word.length();
if (len < minLen) {
minLen = len;
}
}
return minLen == Integer.MAX_VALUE ? 0 : minLen;
}
public static void main(String[] args) {
String text = "Find the shortest word length in this string";
int result = shortestWordLength(text);
System.out.println("Shortest word length: " + result);
}
}
/*
run:
Shortest word length: 2
*/