第11次作业--字符串处理
题目1:编写一个应用程序,统计输入的一个字符串中相同字符的个数,并将统计结果输出。
package zuoyeshiyi; import java.util.*; public class zyshiyi { public static void main(String[] args) { // TODO Auto-generated method stub Scanner reader=new Scanner(System.in); String str=reader.nextLine(); char[] c=str.toCharArray(); int num=0; Map<Character,Integer> hm=new HashMap<Character,Integer>(); for(int x=0;x<c.length;x++) { num=0; for(int y=0;y<c.length;y++) { if(c[x]==c[y]) num++; } hm.put(c[x], num); } Set<Map.Entry<Character,Integer>> s=hm.entrySet(); Iterator<Map.Entry<Character,Integer>> it=s.iterator(); while(it.hasNext()) { Map.Entry<Character,Integer> me=it.next(); Character ct=me.getKey(); Integer ig=me.getValue(); System.out.println(ct+" "+ig); } } }
题目2:编写程序,输入一个字符串,判断该串中的字母能否组成一个回文串(回文串:一个字符串从前向后读取和从后向前读取都一样)。如:ab<c>c?ba
package zuoyeshiyi; import java.util.Scanner; public class zysy { public static void main(String[] args) { // TODO Auto-generated method stub Scanner reader=new Scanner(System.in); for(;;) { String s=reader.nextLine(); System.out.println(huiwen(s)); } } public static boolean huiwen(String s) { char[] c=s.toCharArray(); int start = 0; int end = c.length-1; while(start<end) { if(c[start]==c[end]) { start++; end--; }else return false; } return true; } }