In: Computer Science
Create a class that calls for user to input a string. Pass the string from that class to another and count how many words the string has, count how many vowels are in the string and count how many letters end with d.
java language
//TestCode.java import java.util.Scanner; public class TestCode { public static int countWords(String s){ return s.split(" ").length; } public static int countVowels(String s){ int count = 0; char ch; for(int i = 0;i<s.length();i++){ ch = s.charAt(i); if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'){ count += 1; } } return count; } public static int countWordsEndsWithD(String s){ String[] splits = s.split(" "); int count = 0; for(String x: splits){ if(x.endsWith("d")){ count += 1; } } return count; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s; System.out.print("Enter string: "); s = scanner.nextLine(); System.out.println("Number of words = "+countWords(s)); System.out.println("Number of vowels = "+countVowels(s)); System.out.println("Number of words ends with letter d = "+countWordsEndsWithD(s)); } }