对字符串操作的各种笔试题

  下面列出一些在笔试中常出现的对字符串操作的题目,都是本人自己写的,如果哪里不对或者有更好的实现欢迎大家指出!如果有其他题目也欢迎大家贴出来!谢谢!

一、实现字符串的反转,如输入"abc",返回"cba"

 1 package com.others;
 2 
 3 public class 字符串反转 {
 4 
 5     public static void main(String[] args) {
 6         System.out.println(inverse("liuling"));
 7     }
 8     
 9     public static String inverse(String str){
10         char[] chars = str.toCharArray(); //得到字符数组
11         for (int i = 0; i < chars.length/2; i++) {
12             char temp = chars[i];
13             chars[i] = chars[chars.length-i-1];
14             chars[chars.length-i-1] = temp;
15         }
16         
17         return String.copyValueOf(chars);
18     }
19 
20 }

 

二、找字符串中最长对称串

 1 package com.others;
 2 
 3 
 4 public class 找字符串中最长对称串 {
 5 
 6     public static void main(String[] args) {
 7         System.out.println(findSymmetry("dgaabcddcbadfabcdefghijkkjihgfedcbagaaabbccddddccbbaaaf"));
 8     }
 9     /**
10      * 找最长对称字符串,应该重最大长度开始找,找到就返回
11      * 如果从最小开始找的话,效率好低,那找到一个还要继续找,直到找到最长的为止
12      * 如果找对称的个数的话那就另当别论了,必须得所有都判断
13      */
14     public static String findSymmetry(String str){
15         String symmetryStr; 
16         for (int i = str.length()-1; i > 0; i--) { //找是否有长度为i的对称串,i从最大开始
17             for (int j = 0; j) {
18                 symmetryStr = str.substring(j, j+i+1);
19                 char[] chars = symmetryStr.toCharArray();
20                 int k;
21                 //判断是否为对称串
22                 for (k = 0; k < chars.length/2; k++) {
23                     if(chars[k] != chars[chars.length-k-1]){
24                         break;
25                     }
26                 }
27                 //如果对称则返回
28                 if(k == chars.length/2){
29                     return symmetryStr;
30                 }
31             }
32         }
33         return "";
34     }
35 }

 

三、求字符串中对称串的个数

 1 package com.others;
 2 
 3 public class 求字符串中对称串的个数 {
 4 
 5 
 6     public static void main(String[] args) {
 7         System.out.println(findSymmetryCount("aabbaaffddphpaffa"));
 8     }
 9     public static int findSymmetryCount(String str){
10         int count = 0;
11         String symmetryStr; 
12         for (int i = 1; i < str.length(); i++) {
13             for (int j = 0; j) {
14                 symmetryStr = str.substring(j, j+i+1);
15                 char[] chars = symmetryStr.toCharArray();
16                 int k;
17                 //判断是否为对称串
18                 for (k = 0; k < chars.length/2; k++) {
19                     if(chars[k] != chars[chars.length-k-1]){
20                         break;
21                     }
22                 }
23                 //如果对称则count+1
24                 if(k == chars.length/2){
25                     count++;
26                 }
27             }
28         }
29         return count;
30     }
31 }

 

四、求字符串中出现频率最高的字符

 1 package com.others;
 2 
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 
 6 public class 求字符串中出现频率最高的字符 {
 7 
 8     public static void main(String[] args) {
 9         System.out.println(findHighRateChar("abcdfeeafdaf"));
10     }
11     public static char findHighRateChar(String str){
12         int max = 0;
13         char c = ' ';
14         Map map = new HashMap();
15         char[] chars = str.toCharArray();
16         for (int i = 0; i < chars.length; i++) {
17             if(map.containsKey(chars[i])){
18                 map.put(chars[i], map.get(chars[i])+1);
19                 if(map.get(chars[i])>max){
20                     max = map.get(chars[i]);
21                     c = chars[i];
22                 }
23             }else{
24                 map.put(chars[i], 1);
25             }
26         }
27         return c;
28     }
29 }

 


有待补充......

转载于:https://www.cnblogs.com/liuling/p/2013-8-11-01.html

你可能感兴趣的:(java)