JavaAlgorithms/Others/CountWords.java
yanglbme 29948363da docs: update the whole repository
* fix some bugs
* delete duplicate files
* format code
2019-05-09 19:32:54 +08:00

29 lines
659 B
Java

package Others;
import java.util.Scanner;
/**
* You enter a string into this program, and it will return how many words were
* in that particular string
*
* @author Marcus
*/
public class CountWords {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your text: ");
String str = input.nextLine();
System.out.println("Your text has " + wordCount(str) + " word(s)");
input.close();
}
private static int wordCount(String s) {
if (s == null || s.isEmpty())
return 0;
return s.trim().split("[\\s]+").length;
}
}