Skip to content

Commit 23366ea

Browse files
committed
added Mateo'sHuffmanCompression\src\com\Main\compression\count_chars.java
1 parent 708d038 commit 23366ea

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.Main.compression;
2+
3+
import java.util.Scanner;
4+
5+
public class count_chars{
6+
7+
public static void main(String[] args){
8+
Scanner scan = new Scanner (System.in); // create input object
9+
int count;
10+
int sum;
11+
String str;
12+
char ch;
13+
System.out.println("Type some text");
14+
str = scan.nextLine();
15+
sum=0;
16+
for (ch=32; ch<=254; ch++){ // loop for every printable ASCII char (32 to 254)
17+
count=0;
18+
for (int i=0; i<str.length(); i++){ // loop until the string ends
19+
if ((char)ch == str.charAt(i)){ // check if ASCII char matches string[i]
20+
count++; // how many times each char appears in the string
21+
sum++; // the total
22+
}
23+
}
24+
if (count>0){
25+
if (ch == 32){
26+
System.out.println("[SPACE] = " + count);
27+
}else{
28+
System.out.println((char)ch + " = " + count); // output the count for the corresponding char of the string
29+
}
30+
}
31+
}
32+
System.out.println("Total is " + sum + " bytes"); // output the sum, because variable is used outside of the big loop it shows the total
33+
System.out.println("");
34+
scan.close();
35+
}
36+
37+
}

0 commit comments

Comments
 (0)