一个字母频率分析计算小程序
2004-11-20 19:50
549 查看
//在密码学中有个简单的分析密码的方法,就是计算每个字母出现的频率,这个小程序就是计算输
//入字符串中每个字母出现的次数的,我现在初学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]);
}
}
//入字符串中每个字母出现的次数的,我现在初学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]);
}
}
相关文章推荐
- oracle date 类型字段的处理方法
- java获取Date时间的各种方式汇总
- ATL实现Connection Point的一种简单的方法
- javascript表单之间的数据传递!
- Java测试规范(引用)
- Java面试题目
- delphi中的时间操作技术(1)
- delphi中的时间操作技术(2)
- 解析IP地址为主机域名
- 将某一主机域名解析为IP地址
- 有关TrueDBGrid的问题向各位大虾请教
- 使用C#开发COM+组件
- 初学者的问题----csdn系列
- DOM心得
- Tomcat中通过JNDI配置访问数据库
- log4j的配制及测试程序
- servlet的2种重定向方法
- [转载]我与TOMCAT不得不说的二三事
- date 命令简单记