summaryrefslogtreecommitdiff
path: root/src/algorithmus/Zaehlen.java
blob: 048377e79de6b7b8e15d76876202c81ca60b526f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package algorithmus;

public class Zaehlen extends Algorithmus {
	
	private String name = "Zaehlen";

	@Override
	public void options() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public String encode(String input) {
		input = input.toLowerCase();
		int[][] arr = new int[2][26];
		for(int x = 0; x<arr[0].length; x++) {
			arr[0][x] = 97+x;
			arr[1][x] = 0;
		}
		for(int i = 0, size = input.length(); i<size; i++)
			for(int x = 0; x<arr[0].length; x++)
				if((int)input.charAt(i) == arr[0][x])
					arr[1][x]++;
		String out = "";
		for(int i = 0; i<arr[0].length; i++)
			out += (char)arr[0][i] + ": " + arr[1][i] + "\n";
		return out;
	}

	@Override
	public String decode(String input) {
		String[] arr = input.split(": ");
		int[][] a = new int[2][26];
		for(int x = 0; x<a[0].length; x++) {
			a[0][x] = 97+x;
			a[1][x] = 0;
		}
		for(int i = 0, x = 1; i<a[0].length; i++) {
			a[1][i] = Integer.parseInt(arr[x].substring(0, arr[x].length()-2));
			x++;
		}
		String out = "";
		for(int i = 0; i<a[0].length; i++)
			for(int x = 0; x<a[1][i]; x++)
				out += (char)a[0][i];
		return out.toUpperCase();
	}

}