当前位置: 代码迷 >> Eclipse >> 统计在tt字符串中"a"到"z"26个字母各自出现的次数,该怎么解决
  详细解决方案

统计在tt字符串中"a"到"z"26个字母各自出现的次数,该怎么解决

热度:709   发布时间:2016-04-23 02:03:42.0
统计在tt字符串中"a"到"z"26个字母各自出现的次数
请编写一个函数 void fun(char *tt,int pp[]),统计在tt字符串中"a"到"z"26个字母各自出现的次数,并依次放在pp所指数组中。 
     例如,当输入字符串abcdefghabcdeabc后,程序的输出结果应该是:3 3 3 2 2 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

求这个方法!
------最佳解决方案--------------------
引用:
Java code



1234567891011121314151617181920

public static void main(String[] args) {     Scanner rd=new Scanner(System.in);     String str=rd.next();     int[] alpha26=new int[27];        // 其……

为什么输出会是[3, 3, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]这样的,可不可以去掉【】和逗号的 
------其他解决方案--------------------
你是不是发贴发错板块了?从你定义来看是 C,这个是Java板块。

做法倒是很简单,对 tt 进行循环处理,直到碰到 \0

然后将:当前字符-'a' 就得到数组下标了,直接进行++运算即可。
------其他解决方案--------------------
引用:
你是不是发贴发错板块了?从你定义来看是 C,这个是Java板块。

做法倒是很简单,对 tt 进行循环处理,直到碰到 \0

然后将:当前字符-'a' 就得到数组下标了,直接进行++运算即可。

我要的就是java啊
------其他解决方案--------------------
String[] str = new String[] { "a", "b", "c", "d", "e", "f", "g", "h",
"i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z" };
int[] st = new int[str.length];
Scanner input = new Scanner(System.in);
System.out.println("请输入:");
String s = input.next();
for (int i = 0; i < 26; i++) {
int count = 0;
for (int j = 0; j < s.length(); j++) {

if (s.substring(j, (j + 1)).equals(str[i])) {
count++;
}
}
System.out.print(count + " ");
st[i] = count;
}
------其他解决方案--------------------
请输入:
abcabcacb
3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
------其他解决方案--------------------
	public static void main(String[] args)
{
Scanner rd=new Scanner(System.in);
String str=rd.next();
int[] alpha26=new int[27]; // 其他字符放在 26处。
char temp;
for(int i=0,length=str.length();i<length;i++)
{
temp=str.charAt(i);
if(temp<'a'
------其他解决方案--------------------
temp>'z')
{
alpha26[26]++;
}
else
{
alpha26[temp-'a']++;
}
}
System.out.println(Arrays.toString(alpha26));
  相关解决方案