Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JaisonBrooks/b00310f5bbd094b16cb1 to your computer and use it in GitHub Desktop.
Save JaisonBrooks/b00310f5bbd094b16cb1 to your computer and use it in GitHub Desktop.
pretty clevor
public static void main(String[] args) {
String inputStr;
String charInput;
int totalCount = 0;
int index = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter text to be searched: ");
inputStr = scanner.nextLine();
System.out.println("Enter a character: ");
charInput = scanner.nextLine();
while (index > -1) {
index = inputStr.indexOf(charInput, index + 1);
if (index > -1) {
totalCount++;
}
}
System.out.println("Number of Letter (" + charInput + ") Occurrences = " + totalCount);
String indexes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toLowerCase();
int[] count = new int[indexes.length()];
for (int i = 0; i < inputStr.length(); i++) {
int newIndex = indexes.indexOf(inputStr.charAt(i));
if (newIndex < 0)
continue;
count[newIndex]++;
}
for (int i = 0; i < indexes.length(); i++) {
System.out.println(String.format("%s (%d) %s",
indexes.charAt(i),
count[i],
new String(new char[count[i]]).replace('\0', '*')));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment