Skip to content

Instantly share code, notes, and snippets.

@narate
Created June 19, 2014 09:43
Show Gist options
  • Save narate/c6d4e13bb1c00354e035 to your computer and use it in GitHub Desktop.
Save narate/c6d4e13bb1c00354e035 to your computer and use it in GitHub Desktop.
Count chars in Java
import java.util.Random;
public class CountChars {
static char[] chars;
static int[] count;
static String[][] graph;
public static void main(String args[]) {
chars = new char[100];
count = new int[26];
rand_char(chars);
for(int i = 0; i < chars.length; i++) {
/*if(i % 52 == 0) {
System.out.println();
}
System.out.printf("%c ",chars[i]);*/
count[(int)chars[i] - 'a']++;
}
System.out.println("\n");
graph = new String[max(count) + 2][count.length];
for(int i = 0; i < graph.length; i++) {
for(int j = 0; j < graph[i].length; j++) {
graph[i][j] = " ";
}
}
// assign value to graph
for(int i = graph.length - 1; i > 0; i--) {
for(int j = 0; j < graph[0].length; j++) {
for(int k = 0; k < count[j]; k++) {
graph[graph.length - 1 - k][j] = " *";
}
//graph[graph.length - 1 - count[j]][j] = String.format("%2d", count[j]);
}
}
for(int i = 0; i < graph.length; i++) {
for(int j = 0; j < graph[0].length; j++) {
System.out.print(" " + graph[i][j] + " ");
}
System.out.println();
}
for(int i = (int)'a'; i <= (int)'z'; i++) {
System.out.print("----");
}
System.out.println();
for(int i = (int)'a'; i <= (int)'z'; i++) {
System.out.printf(" %c ", (char)i);
}
System.out.println();
}
public static int max(int[] c) {
int max = c[0];
for(int e: c ) {
max = (e > max) ? e : max;
}
return max;
}
public static void rand_char(char[] c) {
Random rand = new Random();
for(int i = 0; i < c.length; i++) {
c[i] = (char)(rand.nextInt((int)'z' - (int)'a' + 1) + (int)'a');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment