No title
November 15, 2023
0
public class WordCount {
public static int countWords(String text) {
if (text == null || text.isEmpty()) {
return 0;
}
String[] words = text.split("\\s+"); // Split the string by whitespace characters
return words.length;
}
public static void main(String[] args) {
String inputText = "This is a sample sentence for word count."; // Replace this with your input text
int wordCount = countWords(inputText);
System.out.println("Word count: " + wordCount);
}
}
