import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
import java.io.BufferedWriter;
import java.io.FileWriter;


public class word5 {

	public static void main(String[] args) {

		System.out.println("Wörter der Länge 5 zählen!");
		System.out.println("Geben Sie den Namen der txt-datei an:");
		
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		
		BufferedReader read = null;
		String inputfile = "";
		String line = "";
		String text = "";
		String[] words = new String[3125];
		int[] count = new int[3125];
		String one = "";
		String two = "";
		String three = "";
		String four = "";
		String five = "";
		int d = 0;

		for (int i = 0; i <= 3124; i++){
				

			if (i <= 624)
				one = "N";
			if ((i > 624) && (i <= 1249))
				one = "A";
			if ((i > 1249) && (i <= 1874))
				one = "T";
			if ((i > 1874) && (i <= 2499))
				one = "G";
			if (i > 2499)
				one = "C";

			if ((i % 625) <= 124)
				two = "N";
			if (((i % 625) > 124) && ((i % 625) <= 249))
				two = "A";
			if ((i % 625 > 249) && ((i % 625) <= 374))
				two = "T";
			if (((i % 625) > 374) && ((i % 625) <= 499))
				two = "G";
			if ((i % 625) > 499)
				two = "C";

			if ((i % 125) <= 24)
				three = "N";
			if (((i % 125) > 24) && ((i % 125) <= 49))
				three = "A";
			if ((i % 125 > 49) && ((i % 125) <= 74))
				three = "T";
			if (((i % 125) > 74) && ((i % 125) <= 99))
				three = "G";
			if ((i % 125) > 99)
				three = "C";

			if ((i % 25) <= 4)
				four = "N";
			if (((i % 25) > 4) && ((i % 25) <= 9))
				four = "A";
			if ((i % 25 > 9) && ((i % 25) <= 14))
				four = "T";
			if (((i % 25) > 14) && ((i % 25) <= 19))
				four = "G";
			if ((i % 25) > 19)
				four = "C";

			if ((i % 5) == 0)
				five = "N";
			if ((i % 5) == 1)
				five = "A";
			if ((i % 5) == 2)
				five = "T";
			if ((i % 5) == 3)
				five = "G";
			if ((i % 5) == 4)
				five = "C";


			words[i] = one + two + three + four + five;
			count[i] = 0;

			
		}
	
		try{
			
			inputfile = input.readLine();

			read = new BufferedReader (new FileReader(inputfile));

			while((line = read.readLine()) != null){
				text += line.trim();
				while (text.length() >= 5){
					for (int k = 0; k <= 3124; k++){ 
						if (text.substring(0,5).equals(words[k])){
							count[k] = count[k]+1;
						
						}
					}
					text = text.substring(1, text.length());
				}
				System.out.println("zeile fertig >:" + d);
				d++;
			} 
			
			
			
			
			sortiere(count, words);

			BufferedWriter writer = new BufferedWriter(new FileWriter("QuantityOfWords"));
        			for (int b = 0; b < 3125; b++) {
					if (count[b] != 0){
                  				writer.write(words[b]+": "+count[b]); 
						writer.newLine();
					}
         			}
         		writer.close();		


		} catch (FileNotFoundException e) {
     			 System.out.println("Die Datei wurde nicht gefunden.");
  		} catch (IOException e) {
     			 System.out.println("Bei der Eingabe ist ein Fehler aufgetreten.");
		}

		
	
	}

	//Quick-Sort//

	public static void sortiere(int x[], String y[]) {
	      qSort(x, 0, x.length-1, y);
	}
	    
	public static void qSort(int x[], int links, int rechts, String y[]) {
	      if (links < rechts) {
		 int i = partition(x,links,rechts, y);
		 qSort(x,links,i-1, y);
		 qSort(x,i+1,rechts, y);
	      }
	}
	    
	public static int partition(int x[], int links, int rechts, String y[]) {
	      int pivot, o, p, help;
	      String help2;
	      pivot = x[rechts];               
	      o     = links;
	      p     = rechts-1;
	      while(o<=p) {
		 if (x[o] > pivot) {     
		    // tausche x[o] und x[p]
		    help = x[o]; 
		    x[o] = x[p]; 
		    x[p] = help; 
		    help2 = y[o]; 
		    y[o] = y[p]; 
		    y[p] = help2;                            
		    p--;
		 } else o++;            
	      }
	      // tausche x[o] und x[rechts]
	      help      = x[o];
	      x[o]      = x[rechts];
	      x[rechts] = help;
	      help2 = y[o]; 
	      y[o] = y[rechts]; 
              y[rechts] = help2;
		
	      return o;
	 }
}
