Java获取字符串中字母出现的个数 用java编写一个函数,统计一个字符串中每个字母出现的次数,...
Java\u4e2d\u600e\u4e48\u83b7\u53d6\u5b57\u7b26\u4e32\u91cc\u9762\u7684\u5355\u4e2a\u5b57\u7b26\uff1f\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\u3002
\u968f\u4fbf\u4e00\u79cd\uff1a
String a= "\u4e2d\u56fd\u4eba";
char b=a.charAt(0);
System.out.println(b);
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
/*
* \u9700\u6c42 \uff1a"aababcabcdabcde",\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e00\u4e2a\u5b57\u6bcd\u51fa\u73b0\u7684\u6b21\u6570\u8981\u6c42\u7ed3\u679c:a(5)b(4)c(3)d(2)e(1)
*
* \u5206\u6790\uff1a
* A:\u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u4e32(\u53ef\u4ee5\u6539\u8fdb\u4e3a\u952e\u76d8\u5f55\u5165)
* B:\u5b9a\u4e49\u4e00\u4e2aTreeMap\u96c6\u5408
* \u952e:Character
* \u503c\uff1aInteger
* C:\u628a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4
* D:\u904d\u5386\u5b57\u7b26\u6570\u7ec4\uff0c\u5f97\u5230\u6bcf\u4e00\u4e2a\u5b57\u7b26
* E:\u62ff\u521a\u624d\u5f97\u5230\u7684\u5b57\u7b26\u4f5c\u4e3a\u952e\u5230\u96c6\u5408\u4e2d\u53bb\u627e\u503c\uff0c\u770b\u8fd4\u56de\u503c
* \u662fnull:\u8bf4\u660e\u8be5\u952e\u4e0d\u5b58\u5728\uff0c\u5c31\u628a\u8be5\u5b57\u7b26\u4f5c\u4e3a\u952e\uff0c1\u4f5c\u4e3a\u503c\u5b58\u50a8
* \u4e0d\u662fnull:\u8bf4\u660e\u8be5\u952e\u5b58\u5728\uff0c\u5c31\u628a\u503c\u52a01\uff0c\u7136\u540e\u91cd\u5199\u5b58\u50a8\u8be5\u952e\u548c\u503c
* F:\u5b9a\u4e49\u5b57\u7b26\u4e32\u7f13\u51b2\u533a\u53d8\u91cf
* G:\u904d\u5386\u96c6\u5408\uff0c\u5f97\u5230\u952e\u548c\u503c\uff0c\u8fdb\u884c\u6309\u7167\u8981\u6c42\u62fc\u63a5
* H:\u628a\u5b57\u7b26\u4e32\u7f13\u51b2\u533a\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u8f93\u51fa
*
* \u5f55\u5165\uff1alinqingxia
* \u7ed3\u679c\uff1aresult:a(1)g(1)i(3)l(1)n(2)q(1)x(1)
*/
public class TreeMapDemo {
public static void main(String[] args) {
// \u5b9a\u4e49\u4e00\u4e2a\u5b57\u7b26\u4e32(\u53ef\u4ee5\u6539\u8fdb\u4e3a\u952e\u76d8\u5f55\u5165)
Scanner sc = new Scanner(System.in);
System.out.println("\u8bf7\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\uff1a");
String line = sc.nextLine();
// \u5b9a\u4e49\u4e00\u4e2aTreeMap\u96c6\u5408
TreeMap tm = new TreeMap();
//\u628a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4
char[] chs = line.toCharArray();
//\u904d\u5386\u5b57\u7b26\u6570\u7ec4\uff0c\u5f97\u5230\u6bcf\u4e00\u4e2a\u5b57\u7b26
for(char ch : chs){
//\u62ff\u521a\u624d\u5f97\u5230\u7684\u5b57\u7b26\u4f5c\u4e3a\u952e\u5230\u96c6\u5408\u4e2d\u53bb\u627e\u503c\uff0c\u770b\u8fd4\u56de\u503c
Integer i = tm.get(ch);
//\u662fnull:\u8bf4\u660e\u8be5\u952e\u4e0d\u5b58\u5728\uff0c\u5c31\u628a\u8be5\u5b57\u7b26\u4f5c\u4e3a\u952e\uff0c1\u4f5c\u4e3a\u503c\u5b58\u50a8
if(i == null){
tm.put(ch, 1);
}else {
//\u4e0d\u662fnull:\u8bf4\u660e\u8be5\u952e\u5b58\u5728\uff0c\u5c31\u628a\u503c\u52a01\uff0c\u7136\u540e\u91cd\u5199\u5b58\u50a8\u8be5\u952e\u548c\u503c
i++;
tm.put(ch,i);
}
}
//\u5b9a\u4e49\u5b57\u7b26\u4e32\u7f13\u51b2\u533a\u53d8\u91cf
StringBuilder sb= new StringBuilder();
//\u904d\u5386\u96c6\u5408\uff0c\u5f97\u5230\u952e\u548c\u503c\uff0c\u8fdb\u884c\u6309\u7167\u8981\u6c42\u62fc\u63a5
Set set = tm.keySet();
for(Character key : set){
Integer value = tm.get(key);
sb.append(key).append("(").append(value).append(")");
}
//\u628a\u5b57\u7b26\u4e32\u7f13\u51b2\u533a\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u8f93\u51fa
String result = sb.toString();
System.out.println("result:"+result);
}
}
/***\u4e0d\u61c2\u91cc\u9762\u7684\u7684\u4e00\u4e9b\u65b9\u6cd5\u7684\u53ef\u4ee5\u627e\u672c\u4e66\u770b\u770bMap\u96c6\u5408\u65b9\u9762\u7684\uff0c\u8fd8\u6709\u5b66\u4f1a\u67e5API\uff0c\u5426\u5219\u4f60\u4e00\u8f88\u5b50\u90fd\u8bfb\u4e0d\u61c2JAVA\u7a0b\u5e8f\u7684\uff0c\u5176\u5b9e\u6211\u8fd9\u4e2a\u4e0d\u7528\u5206\u6790\u7684\u8bdd\u5e94\u8be5\u662f\u8fd9\u4e2a\u95ee\u9898\u7684\u6700\u7b80\u89e3\u4e86\u5427\u3002\u3002\u3002\u3002***/
```java
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个字符串:");
String input = sc.nextLine();
HashMap<Character, Integer> map = new HashMap<>();
for (char c : input.toCharArray()) {
if (Character.isLetter(c)) {
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
map.put(c, 1);
}
}
}
for (char key : map.keySet()) {
System.out.println(key + ": " + map.get(key));
}
}
}
```
首先,使用 `Scanner` 获取用户输入的字符串并存储在 `input` 变量中。然后使用 `HashMap` 对每个字母出现次数进行计数,其中 `for` 循环遍历输入字符串中的每个字符,`Character.isLetter(c)` 判断字符是否为字母,如果是则判断其是否已经存在于 `map` 中,若存在则将其出现次数加一,否则将其加入到 `map` 中并将出现次数置为 1。
最后,遍历 `map` 中的元素,并输出每个字母及其出现次数。
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.CyclicBarrier;
public class TestStrNumber {
private static String str1 = "sdfg,hellozx,sdfcv-waadfa,fsasdfxcvdf";
public static void main(String[] args) {
String ss=getUnmatchStr("hello");
getCharNum(ss);
}
public static void getCharNum(String ss){
String sb="";
for (int i = 0; i < ss.length(); i++) {
if (ss.charAt(i)!='-'&&ss.charAt(i)!=',') {
sb+=ss.charAt(i);
}
}
char[] cs=sb.toCharArray();
int oldNum=0;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for (char c : cs) {
if (map.containsKey(c)) {
oldNum=map.get(c);
map.put(c, oldNum+1);
}else {
map.put(c, 1);
}
}
Set<Character> keys = map.keySet();
for(Character c:keys){
System.out.print(c+"(出现"+map.get(c)+"次)");
}
}
public static String getUnmatchStr(String s){
String str2="";
int startIndex=0;
int endIndex=0;
if (str1.contains(s)) {
startIndex = str1.indexOf(s);
endIndex=startIndex+s.length();
}
str2 = str1.substring(0, startIndex)+str1.substring(endIndex, str1.length());
return str2;
}
}
public static void main(String[] args) {
String s = "abc a bc abc bc abc";
Map<String,Integer> map = new HashMap<String,Integer>();
for(String str : s.split("|")){
if(map.containsKey(str)){
map.put(str, map.get(str)+1);
}else{
map.put(str, 1);
}
}
for(Entry<String,Integer> e : map.entrySet()){
System.out.println(e.getKey() +": "+e.getValue()+"个");
}
}
这个有什么难度呢?
绛旓細```棣栧厛锛屼娇鐢 `Scanner` 鑾峰彇鐢ㄦ埛杈撳叆鐨勫瓧绗︿覆骞跺瓨鍌ㄥ湪 `input` 鍙橀噺涓傜劧鍚庝娇鐢 `HashMap` 瀵规瘡涓瀛楁瘝鍑虹幇娆℃暟杩涜璁℃暟锛屽叾涓 `for` 寰幆閬嶅巻杈撳叆瀛楃涓蹭腑鐨姣忎釜瀛楃锛宍Character.isLetter(c)` 鍒ゆ柇瀛楃鏄惁涓哄瓧姣嶏紝濡傛灉鏄垯鍒ゆ柇鍏舵槸鍚﹀凡缁忓瓨鍦ㄤ簬 `map` 涓紝鑻ュ瓨鍦ㄥ垯灏嗗叾鍑虹幇娆℃暟鍔犱竴锛屽惁鍒欏皢鍏...
绛旓細import java.util.*;public class Test { public static void main(String args[]){ String s = "abcdad"; //寰呮祴璇曠殑瀛楃涓 Map<Character, Integer> result = getCharMaps(s);System.out.println(result);//鎵撳嵃鍑哄瓧绗︿覆涓鍚勫瓧绗鍑虹幇鐨娆℃暟锛亇 public static Map<Character, Integer> getCha...
绛旓細1銆佹牴鎹甽ength鑾峰彇瀛楃涓闀垮害 String s = "abcdedfae";//瀹氫箟涓涓瓧绗︿覆int len = s.length();//鑾峰彇鍘熸潵鐨勫瓧绗︿覆闀垮害2銆侀氳繃replaceAll鏂瑰紡锛屾妸瀛楃涓蹭腑璇瀛楁瘝鏇挎崲鎴愮┖ String s1 = s.replaceAll(瑕佺粺璁$殑瀛楁瘝,"");3銆佽幏鍙栨浛鎹㈠悗鐨勫瓧绗︿覆闀垮害 int len2 = s1.length();4銆佸師鏉ョ殑瀛楃涓查暱搴...
绛旓細str涓轰綘瑕佹祴璇曠殑瀛楃涓茬涓绉嶆柟娉曪細byte[]temp=str.getbytes();//浣跨敤骞冲彴榛樿鐨勫瓧绗﹂泦灏嗘string瑙g爜涓哄瓧鑺傚簭鍒楋紝骞跺皢缁撴灉瀛樺偍鍒颁竴涓柊鐨勫瓧鑺傛暟缁勪腑銆俰ntcount=0;//閬嶅巻鏁扮粍鐨勬瘡涓涓厓绱狅紝涔熷氨鏄瀛楃涓蹭腑鐨姣忎竴涓瀛楁瘝for(inti=0;i<temp.length;i++){//濡傛灉瀛楁瘝绛変簬cif(temp[i].equals('c'...
绛旓細import java.util.Set;import java.util.TreeMap;/ 闇姹 锛"aababcabcdabcde",鑾峰彇瀛楃涓蹭腑姣忎竴涓瀛楁瘝鍑虹幇鐨娆℃暟瑕佹眰缁撴灉:a(5)b(4)c(3)d(2)e(1)鍒嗘瀽锛欰:瀹氫箟涓涓瓧绗︿覆(鍙互鏀硅繘涓洪敭鐩樺綍鍏)B:瀹氫箟涓涓猅reeMap闆嗗悎 閿:Character 鍊硷細Integer C:鎶婂瓧绗︿覆杞崲涓哄瓧绗︽暟缁 D:閬嶅巻瀛楃鏁扮粍锛屽緱鍒...
绛旓細1銆佹枃浠--銆嬫柊寤--銆嬮」鐩2銆侀夋嫨涓猴細鎺у埗鍙板簲鐢ㄧ▼搴--銆嬪懡鍚嶏細缁熻涓涓瀛楃涓蹭腑姣忎釜灏忓啓瀛楁瘝鍑虹幇鐨娆℃暟--銆嬬‘瀹氥3銆佺‘瀹氬悗绯荤粺鐢熸垚鐨勪唬鐮併4銆佸厛鍐欎竴涓瓧绗︿覆鐢ㄤ簬娴嬭瘯銆5銆佷娇鐢―ictionary闆嗗悎鐒跺悗寰幆鍒ゆ柇娴嬭瘯浠g爜銆6銆佺▼搴忚繍琛屾祴璇曟垚鍔熷悗鏄剧ず姣忕瀛楁瘝鍑虹幇鐨勬鏁般
绛旓細1銆佷娇鐢 String.replace() 鎶婄洰鏍瀛楁瘝鏇挎崲鎺夛紝鐒跺悗绠瀛楃涓闀垮害鐨勫彉鍖栥2銆乻ubstring()鏂规硶锛岀湅鐪鍑虹幇浜嗗嚑涓暟缁-1銆Java鏄竴绉嶅彲浠ユ挵鍐欒法骞冲彴搴旂敤绋嬪簭鐨勯潰鍚戝璞$殑绋嬪簭璁捐璇█銆侸ava 鎶鏈叿鏈夊崜瓒婄殑閫氱敤鎬с侀珮鏁堟с佸钩鍙扮Щ妞嶆у拰瀹夊叏鎬э紝骞挎硾搴旂敤浜嶱C銆佹暟鎹腑蹇冦佹父鎴忔帶鍒跺彴銆佺瀛﹁秴绾ц绠楁満銆佺Щ鍔ㄧ數璇濆拰...
绛旓細import java.util.HashMap;import java.util.Map;public class RemoveMostWords { public static void main(String[] args){ String str = "addcbbs";System.out.println("澶勭悊鍓嶇殑瀛楃涓锛" + str);String deelStr = removeMostWords(str);System.out.println("澶勭悊鍚庣殑瀛楃涓诧細" + deelStr)...
绛旓細绗竴绉嶆柟娉曪細byte[] temp = str.getBytes();//浣跨敤骞冲彴榛樿鐨勫瓧绗﹂泦灏嗘 String 瑙g爜涓哄瓧鑺傚簭鍒楋紝骞跺皢缁撴灉瀛樺偍鍒颁竴涓柊鐨勫瓧鑺傛暟缁勪腑銆俰nt count = 0;//閬嶅巻鏁扮粍鐨勬瘡涓涓厓绱狅紝涔熷氨鏄瀛楃涓蹭腑鐨姣忎竴涓瀛楁瘝 for(int i=0;i<temp.length; i++){ //濡傛灉瀛楁瘝绛変簬c if(temp[i].equals('c'...
绛旓細import java.io.*;public class ReadFile{ public static void show(String s){ int len = s.length();int letterCount = 0; //鑻辨枃瀛楁瘝涓暟 int blankCount = 0; //绌烘牸涓暟 int numCount = 0; //鏁板瓧涓暟 int otherCount = 0; //鍏朵粬瀛楃涓暟 for(int i = 0;i < len;i...