java对字符串的操作_[Java教程]对字符串操作的各种笔试题

[Java教程]对字符串操作的各种笔试题

0 2013-08-11 21:00:57

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

一、实现字符串的反转,如输入"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

三、求字符串中对称串的个数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

四、求字符串中出现频率最高的字符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 }

有待补充......

本文网址:http://www.shaoqun.com/a/67833.html

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:admin@shaoqun.com。

0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值