public class test9 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String src=sc.nextLine();
judge(src);
}
static void judge(String src){
int Rem=0; //记录重复字符数
int[] ch=new int[128]; //用于记录重复的字符
for (int i = 0; i < src.length(); i++) {
int index=(int)src.charAt(i); //将字符转化为ASCill码,对应的index就是字符的ASCILL码
ch[index]++; //巧用下标,将字符的ASCILL码作为辅助数组的下标,将其对应位置+1,记录出现字符的次数
}
for (int i = 0; i < ch.length; i++) { //遍历数组
if (ch[i]>1) { //字符个数超过一个,字符重复
System.out.println("字符"+(char) i + "重复");
Rem++;
}
}
if (Rem==0)
System.out.println("无字符重复");
}
}
判断字符串中有无重复字符——巧用ASCILL码
最新推荐文章于 2023-06-29 09:59:58 发布