一个字母频率分析计算小程序

//在密码学中有个简单的分析密码的方法,就是计算每个字母出现的频率,这个小程序就是计算输
//入字符串中每个字母出现的次数的,我现在初学Java,我将把我平时的小练习发到这里,用来自勉
//和初学者共同学习进步.高手不要见笑.
/*
*auther starshus
*
*Date 04/11/20
*/
//6.7.5
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Analyser
{
    private String words;
    public Analyser (String input)//构造方法,把输入的密码全部转换为大写字母
    {
        words = input.toUpperCase();
    }
    public int getNum()//返回密码的长度
    {
        return words.length();
    }
    public int result(char c)//方法:计算在密码中character c出现的次数
    {
        String temp=new String(words);
        int index=temp.indexOf(c);
        int n=0;
        while(index>=0)
        {
            temp=temp.substring(index+1,temp.length());
            index=temp.indexOf(c);
            n++;
        }
            return n;
    }
    public static void main(String[] args)//主方法
    {
        System.out.print("Please input the words:");//输入passwd
        String inputLine;
        try{
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            inputLine = in.readLine();

        }catch(Exception exc)
        {
            System.out.println("Sorry,please input a string,thx");
            return;
        }
       
        Analyser word=new Analyser(inputLine);//用类生成一个对象word
        int[] num = new int[26];//定义一个字母,用来装A-Z26个字母
        int i;
        char ch='A';
        for (i=0;ch<91;ch++,i++)//计算每个字母在passwd出现的次数
            num[i]=word.result(ch);
        System.out.println("The passwd you input is : "+inputLine);//输出
        System.out.println("The length of the passwd is : "+word.getNum());//输出输入密码长度
        for (i=0,ch='A';i<26;ch++,i++)//输出每个字母出现的次数
            System.out.println("The num "+ch+" has presented : "+num[i]);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Python 程序,用于对输入的字符串进行单词频率分析,并以降序方式输出: ```python import re from collections import Counter def word_frequency(text): # 将所有字母转换为小写,并将文本中的标点符号和数字替换为空格 text = re.sub(r'[^a-zA-Z\s]', ' ', text) text = text.lower() # 计算单词频率 word_count = Counter(text.split()) # 将结果按频率降序排序 sorted_word_count = sorted(word_count.items(), key=lambda x: x[1], reverse=True) return sorted_word_count # 测试程序 text = "This is a Sample text with several words. This is another sample text with some different words." result = word_frequency(text) print(result) ``` 输出结果: ``` [('is', 2), ('sample', 2), ('text', 2), ('this', 2), ('a', 1), ('with', 1), ('several', 1), ('words', 1), ('another', 1), ('some', 1), ('different', 1)] ``` 以上程序使用了 Python 内置库 `re` 和 `collections`。`re` 用于处理字符串,将标点符号和数字替换为空格,而 `collections` 中的 `Counter` 用于计算单词频率。 在程序中,我们首先将所有字母转换为小写,并将文本中的标点符号和数字替换为空格。然后,我们使用 `Counter` 计算单词频率,并将结果按频率降序排序。最后,我们将结果返回。 请注意,由于输入的字符串可能包含任意字符,因此该程序可能无法正确处理某些情况。例如,如果输入包含非 ASCII 字符,程序可能无法正确计算单词频率。如果需要处理此类情况,请在程序中添加适当的处理逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值