import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.Map;
import java.util.HashMap;
// Reads user input
class InputReader {
public static String prompt() {
Scanner scanner = new Scanner(System.in);
System.out.printf("Input String: ");
String input = scanner.nextLine();
scanner.close();
return input;
}
}
// Analyzes a string's characteristics
class StringAnalyzer {
public static void parse(String word) {
printDuplicates(word);
System.out.printf("Vowels: %d \n", vowels(word));
System.out.printf("Consonants: %d \n", consonants(word));
System.out.printf("Digits: %d \n", digits(word));
System.out.printf("Spaces: %d \n", spaces(word));
System.out.printf("Special Characters: %d \n", special(word));
System.out.println("Process completed.");
}
private static void printDuplicates(String word) {
Map<Character, Integer> dupes = countAndMapCharacters(word).entrySet()
.stream()
.filter(entry -> entry.getValue() > 1)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
System.out.printf("Characters with duplicates: %d \n", dupes.size());
dupes.entrySet()
.stream()
.forEach(entry ->
System.out.printf(
"Character: %s : Count %d \n",
entry.getKey(),
entry.getValue()));
}
private static int vowels(String word) {
return word.replaceAll("[^aeiouAEIOU]", "").length();
}
private static int consonants(String word) {
return word.replaceAll("[^b-df-hj-np-tv-z]", "").length();
}
private static int digits(String word) {
return word.replaceAll("[^0-9]", "").length();
}
private static int spaces(String word) {
return word.replaceAll("[^ ]", "").length();
}
private static int special(String word) {
return word.length()
- vowels(word)
- consonants(word)
- digits(word)
- spaces(word);
}
// Utility function which counts how many times a character has been seen
private static Map<Character, Integer> countAndMapCharacters(String word) {
Map<Character, Integer> map = new HashMap();
word.chars().forEach(item -> {
Integer currentCount = map.get((char) item);
if (currentCount == null) {
map.put((char) item, 1);
} else {
map.put((char) item, currentCount + 1);
}
});
return map;
}
}
class Main {
public static void main(String[] args) {
StringAnalyzer.parse(InputReader.prompt());
}
}