Skip to content

Instantly share code, notes, and snippets.

@prongbang
Last active August 29, 2015 13:57
Show Gist options
  • Save prongbang/9637711 to your computer and use it in GitHub Desktop.
Save prongbang/9637711 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class CountCharacter{
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.print("Enter Name : ");
String name = scn.nextLine();
int len = name.length();
int[][] ascii = new int[len][2];
int in = 0, on = 0, p = 0;
/* Preprocessing */
for (p =0;p < len; p++ ){
ascii[p][0] = (int)name.charAt(p);
ascii[p][1] = 0;
}
CountCharacter H = new CountCharacter();
/* Searching Countting Character */
H.CounttingCharacter(ascii,len);
/* Print */
int count = 0;
for (on = 0;on < len; on++ ){
if (ascii[on][0] != ' '){
count = H.FormatAscii(ascii,len,on);
if (count > 0)
System.out.println((char)ascii[on][0] + " = " + ascii[on][1]);
else
if ((char)ascii[in][0] != ' ')
System.out.println((char)ascii[on][0] + " = " + ascii[on][1]);
}
} // for on
System.out.println();
} // main
public void CounttingCharacter(int ascii[][],int len){
int on = 0, in = 0;
for (on = 0;on < len; on++ )
for (in =0;in < len; in++)
if (ascii[on][0] == ascii[in][0])
ascii[on][1]++;
}
public int FormatAscii(int[][] ascii,int len,int on){
int count = 0, in = 0;
for (in = on+1; in < len; in++ ){
if (ascii[on][0] == ascii[in][0]){
count++; /* นับตัวที่ซ้ำกัน */
ascii[in][0] = (int)' '; /* set ค่าให้เป็น Space */
}
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment