⑴ java:輸入一個純字母字元串,求出現頻率最多的那個字母和出現的次數
你好,代碼如下:
/**
* 統計字元串中出現最多的字母及其次數
* @author gogole_09
*
*/
public class CountChar {
private int[] charCount = new int[26]; // 保存字母出現的次數,初步只統計26個字母(都轉換成小寫字母),
/**
* 統計字母出現次數
*
* @param str
*/
private void countChar(String str) {
str = str.toLowerCase();
// 字母a-z的索引
for (char chari = 'a'; chari <= (char) ('a' + 25); chari++) {
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (chari == ch) {
charCount[chari - 'a']++;
}
}
}
}
/**
* 計算出現最多的字母
* @return
*/
private char getMaxChar() {
char max = '?';
int maxcount = 0;
for (char chari = 'a'; chari <= (char) ('a' + 25); chari++) {
if (charCount[chari - 'a'] >= maxcount) {
maxcount = charCount[chari - 'a'];
max = chari;
}
}
return max;
}
/**
* 獲取出現最多的字母出現的具體次數
* @return
*/
private int getMaxCount(){
return getMaxChar()=='?'?charCount[getMaxChar()-'a']:-1;
}
public static void main(String[] args) {
String str="aaabab";
CountChar c=new CountChar();
c.countChar(str);
System.out.println(str+":中出現次數最多的字母是:");
System.out.println(c.getMaxChar());
System.out.println("出現次數是:");
System.out.println(c.getMaxCount());
}
}
⑵ 怎麼用c語言統計一個字元串中出現最多的字母,並輸出這個字元出現的次數
摘要 構建一個循環,從頭到尾掃描字元串,比如abcabcd,首先判斷所掃描的字元是否出現過,若沒出現過,構建一個變數,初始值為一(變數與字母一一對應,變數的值即為該字元出現的次數);若出現過,使其所對應的變數加一。循環結束以後,比較這幾個變數的大小,可以用冒泡法或快排,將最大值輸出。具體程序並不復雜,相信樓主可以完成,樓下那個開數組的方法比較浪費內存,不建議使用。
⑶ 用java實現:輸入一串字母的字元串,求出現最多的那個字母,出現了多少次
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String s = new Scanner(System.in).next(); //讀入輸入的字元串
List<String> chars=Arrays.asList(s.split("")); //把字元串分解成一個個字元
HashMap<String,Integer> map=new HashMap<String,Integer> (); //建立一個map key為字元 value為key出現的次數
int max=0;String maxChar=null; //maxChar為出現最多的字元,max為次數
for(String i:chars){
int cs=map.get(i)==null?0:map.get(i); //取出字元所對應的次數
cs++; //次數加1
if(cs>max){ //如果當前次數為最大
maxChar=i;
max=cs;
}
map.put(i, cs); //改變map中的次數
}
System.out.println(map+":"+"\n"+maxChar+":"+max);
}
}
⑷ JAVA如何找出字元串中出現最多的字母
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);
}
private static String removeMostWords(String str)
{
if (null == str || "".equals(str))
{
return str;
}
//初始化最大次數為2次
int iMax = 2;
//將所有的字元與出現的次數作為一個鍵值對
Map<Character, Integer> timeMap = new HashMap<Character, Integer>();
Character cTmp;
Integer iTmp;
for (int i = 0; i < str.length(); i++)
{
cTmp = str.charAt(i);
iTmp = timeMap.get(cTmp);
//首次出現的字元,不需要比較
if (null == iTmp)
{
timeMap.put(cTmp, 1);
continue;
}
//出現次數+1,放入map
iTmp = iTmp + 1;
timeMap.put(cTmp, iTmp);
//若超過最大次數,則替換最大次數
iMax = iMax < iTmp ? iTmp : iMax;
}
//將所有達到最大次數的字元替換成空
for (Map.Entry<Character, Integer> entry : timeMap.entrySet())
{
if (entry.getValue() == iMax)
{
str = str.replaceAll(String.valueOf(entry.getKey()), "");
}
}
return str;
}
}
⑸ C語言編程題目 給一個字元串,請找出出現次數最多的大寫英文字母。
#include<stdio.h>
int main()
{
char ch;
int max,c[26]={0};
while((ch=getchar())!='\n')
if(ch>='A'&&ch<='Z')
{
++c[ch-'A'];
if(c[ch-'A']>c[max])
max=ch-'A';
}
if(c[max])
printf("%c:%d\n",(char)(max+'A'),c[max]);
else
printf("字元串不包含任何大寫字母\n");
return 0;
}
⑹ 找出字元串中出現次數最多的字元 c語言
#include<iostream>
#include<string>
using namespace std;
void main(void)
{
char testStr[100] = { "We are happppppppppy." };
int aToZ[256]; //初始化一個 int aToZ[256]並清零,對應256個字元的ASCII值;
int maxArrayNum;
int count = 256;
while (count--)
{
aToZ[count] = 0;
}
//遍歷字元串數組,並相應在aToZ[i]中計數;
for (int i = 0; testStr[i] != '