Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
2.5 kB
4
Indexable
Never
import java.util.Locale;

public class work_with_text {

    public static void main(String[] args) {
        String text = "Here you can find activities to practise your reading skills";

        String[] splitText = text.split(" ");

        String word = "find";

        System.out.println(text);

        System.out.println("1. The word in the text is... " + contains(word, splitText));
        System.out.println("2. Count words in text: " + countWords(splitText));
        System.out.println("3. Count chars in text without space: " + withoutSpace(splitText));
        System.out.println("4. Count chars in text without space: " + withSpace(splitText));
        System.out.println("5. Count vowels: " + countVowels(splitText));
        System.out.println("5. Count Consonants: " + countConsonants(splitText));
    }

    public static boolean contains(String word, String[] text) {
        int size = text.length;
        for (int i = 0; i < size; i++) {
            if (text[i].toUpperCase(Locale.ROOT).equals(word.toUpperCase())) {
                return true;
            }
        }
        return false;
    }

    public static int countWords(String[] text) {
        return text.length;
    }

    public static int withoutSpace(String[] text) {
        int count = 0;
        int size = text.length;
        for (int i = 0; i < size; i++) {
            count += text[0].length();
        }
        return count;
    }

    public static int withSpace(String[] text) {
        return withoutSpace(text) + text.length - 1;
    }

    public static int countVowels(String[] text) {
        String vowels = "AEIOUY";
        int size = text.length;
        int count = 0;
        for (int i = 0; i < size; i++) {
            for (char c : vowels.toCharArray()) {
                if (text[i].toUpperCase(Locale.ROOT).contains(String.valueOf(c))) {
                    count += 1;
                }
            }
        }
        return count;
    }

    public static int countConsonants(String[] text) {
        String consonants = "BCDFGHJKLMNPQRSTVWXYZ";
        int size = text.length;
        int count = 0;
        for (int i = 0; i < size; i++) {
            for (char c : consonants.toCharArray()) {
                if (text[i].toUpperCase(Locale.ROOT).contains(String.valueOf(c))) {
                    count += 1;
                }
            }
        }
        return count;
    }
}