Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- String str = "The has !been divided abs on98 the issue, with 578934 784,
- +moderates5789 concerned @about the on: the most vulnerable.";
- System.out.println(countWords(str));
- }
- public static int countWords(String input){
- //разбиваю строку на массив слов по пробелу
- int count = 0;
- String[] words = input.split(" ");
- //Это я вывожу в консоль результаты, для наглядности...для себя
- //System.out.println(words.length);
- //System.out.println(Arrays.toString(words));
- //считаю слова, в которых содержатся только буквы
- for (String word : words) {
- if (isValidWord(word)){
- count++;
- }
- }
- return count;
- }
- private static boolean isValidWord(String word) {
- char[] chars = word.toCharArray();
- for (char c : chars) {
- if (!Character.isLetter(c)){
- return false;
- }
- }
- return true;
- }
- String[] words = input.split("\s*[ ,.:]\s*");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement