Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class _05_CountAllWords {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String input = sc.nextLine();
- String textRegEx = "\\w+";
- Pattern textPattern = Pattern.compile(textRegEx);
- Matcher matcher = textPattern.matcher(input);
- int counter = 0;
- while(matcher.find()){
- counter++;
- }
- System.out.println(counter);
- // OR
- // String[] allWords = input.split("\\W+");
- // System.out.println(allWords.length);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment