java统计字符串中每个字符出现的次数,统计字符串中每个字符出现的次数

本文收集整理关于统计字符串中每个字符出现的次数的相关议题,使用内容导航快速到达。

内容导航:

Q1:c语言统计字符串中每个字符出现的次数

一、算法分析:

要统计每个字符出现的个数,那么就要为每个字符做一个统计值,可以用数组实现。

然后输入字符串。

遍历字符串,对每个字符进行统计。

输出结果。

二、参考代码:#includeintmain()

{

intcnt[128]={0};//用来统计个数。

charstr[200];//存储字符串。

inti;

gets(str);//输入字符串。

for(i=0;str[i]!=\0;++i)//遍历字符串。

cnt[str[i]]++;//统计个数。

for(i=0;i<128;i++)//遍历统计到的值。

if(cnt[i]!=0)//如果出现过则打印值,及个数。

printf("%c:%d\n",i,cnt[i]);//输出结果。

return0;

}

Q2:java如何统计字符串中每个字符出现的次数

正确答案:

import java.util.Iterator;

import java.util.Map;

import java.util.TreeMap;

public class Test {

@SuppressWarnings("unchecked")

public static void main(String[] args) {

String str = null;

try {

str = args[0];

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println("请输入参数!");

System.exit(0);

}

Map tree = new TreeMap();

for (int i = 0; i < str.length(); i++) {

char ch = str.charAt(i);

if ((ch >= a && ch <= z)(ch >= A && ch <= Z)) {

if (!tree.containsKey(ch)) {

tree.put(ch, new Integer(1));

} else {

Integer in = (Integer) tree.get(ch) + 1;

tree.put(ch, in);

}

}

}

Iterator tit = tree.keySet().iterator();

while (tit.hasNext()) {

Object temp = tit.next();

System.out.print(temp.toString() + "(" + tree.get(temp) + ")");

}

}

}

Q3:java中怎么统计一个字符串中每个字符的出现次数?

操作如下:

String str ="2342asfghgyu56asdasda";Mapmaps = new HashMap();for(int i=0;iString key = String.valueOf((str.charAt(i)));if(!maps.containsKey(key)),maps.put(key, 1);else{int val =maps.get(key);maps.put(key, val+1);

for(Map.Entry i : maps.entrySet()){System.out.println(i.getKey()+ "=="+i.getValue());

具体方法:

package com.haotj.demo13;import java.util.Map;import java.util.Set;importjava.util.TreeMap;public class Test,public static void main(String[] args),String str。

"asdlkfjlaksdlkjfdsjlkazxcsdklfwuertiopwrljlflsdalxvclzjlksdfljklsdfuiafjdgllfdgdaslfsdjkldskfjdsl"; Mapmap = countChar(str);。

//遍历//1-Setset = map.keySet();for(Character key : set)System.out.println(key + "=" + map.get(key));//2-//Map中无迭代器,不能如下使用// for(Map.Entryme : map)// {// }Set> entrys = map.entrySet();。

for(Map.Entryme : entrys)System.out.println("===" + me.getKey() + "=" + me.getValue()/*** 统计一个字符串中,每个字符出现的次数。

* * @param str:被统计的字符串,* @return 记录了每个字符及对应该字符出现的次数,* @see [类、类#方法、类#成员]*/public static MapcountChar(String str)

//用于存放字符及对应次数。TreeMaptm = new TreeMap();//得到字符串中的每个字符for(int i = 0; i < str.length(); //取得字符串中每个字符,Character ch = str.charAt(i);

//在tm 对象中,判断该字符是否存在,//如果存在,则取得该key 对应的value 值,将value 值加1,再存入该集合对象,//如果不存在,则将该字符及1,存入到tm 中if(!tm.containsKey(ch)),else。

Q4:C语言编程:输入一字符串,统计字符串中各个字符出现的频率?

//统计一个文件里各个字符出现的次数,转换频率自己再加几句就是了,这里只统计了26个字母,如果还有更多的字符的话,将数组再扩大就行了,wW。W.yIjItAo.cOm应该比较简单,我就不写了

#include#includevoid stat(char *file,int *statistic)

{

int i=0;

while(file[i++]!=0)

statistic[file[i-1]-97]++;

}

int main()

{

char file[100]={0};

int statistic[26]={0};

int i=0,j;

double s=0;

FILE *fp;

if((fp=fopen("1.txt","r"))==NULL)

{

printf("cant open 1.txt");

return 1;

}

while(!feof(fp))

fread(&file[i++],1,1,fp);

fclose(fp);

j=i;

stat(file,statistic);

for(i=0;i<26;i++)

printf("%c:%d\n",97+i,statistic[i]);

return 0;

}

//在源程序目录下建立一个文本文件1.TXT,里面输入字符串,就可以进行统计了

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值