Counting Each Letter in a String

来源:互联网 发布:餐饮库存软件 编辑:程序博客网 时间:2024/05/21 17:54
import java.util.*;


public class Problem {


/**
* @param args
*/
final static int SIZE = 100;


public static void main(String[] args) {
// TODO Auto-generated method stub
char[] chars = new char[SIZE];
int[] list = new int[SIZE];
int size;
Scanner input = new Scanner(System.in);
System.out.print("please input a string : ");
String string = input.nextLine().replace(" ", "").toLowerCase();
size = countChar(string, chars, list);
for (int i = 0; i < size; i++)
if (list[i] == 1)
System.out.println(chars[i] + " appears " + list[i] + " time ");
else
System.out
.println(chars[i] + " appears " + list[i] + " times ");
}


public static int countChar(String s, char[] c, int[] l) {
int j, size = 0;
for (int i = 0; i < s.length(); i++) {
for (j = 0; j < size; j++)
if (s.charAt(i) == c[j]) {
size--;
break;
}
c[j] = s.charAt(i);
l[j]++;
size++;
}
return size;


}


}
原创粉丝点击