星空网 > 软件开发 > Java

字符串测试题1

用java语言实现两个函数encode()和decode(),分别实现对字符串的变换和复原.变换函数encode()顺序考察已知字符串的字符,按以下规则逐组生成新字符串:

(1)若已知字符串的当前字符不是大于0的数字字符,则复制该字符于新字符串中;

(2)若已知字符串的当前字符是一个数字字符,且它之后没有后继字符,则简单地将它复制到新字符串中;

(3)若已知字符串的当前字符是一个大于0的数字字符,并且还有后继字符,设该数字字符的面值为n,则将它的后继字符(包括后继字符是一个数字字符)重复复制n+1次到新字符串中;

(4)以上述一次变换为一组,在不同组之间另插入一个下划线'_'用于分隔;

(5)若已知字符串中包含有下划线'_',则变换为 ”\UL” 。

例如:encode()函数对字符串24ab_2t2的变换结果为 444_aaaaa_a_b_\UL_ttt_t_2

复原函数decode()做变换函数encode()的相反的工作.按照上述规则逆运算,变回原来的字符串。

 1 public class ZiFuChuan { 2  3   // 转换方法 4   public void encode(String str) { 5     String st = ""; 6     for (int i = 0; i < str.length(); i++) { 7       if ((str.charAt(i) >= '0' && str.charAt(i) <= '9') && (i + 1) == str.length()) { 8         st = st + str.charAt(i); 9       } else if ((str.charAt(i) >= '1' && str.charAt(i) <= '9') && (i + 1) != str.length()) {10         int num1 = str.charAt(i) - 48;11         String ls = "";12         for (int j = 0; j <= num1; j++) {13           ls = ls + str.charAt(i + 1);14         }15         st += ls + "_";16       } else if (str.charAt(i) == '_') {17         st += '\\' + "UL" + "_";18       } else {19         if (i != str.length() - 1) {20           st += str.charAt(i) + "_";21         }22         if (i == str.length() - 1) {23           st += str.charAt(i);24         }25       }26     }27     System.out.println("变换后:" + st);28   }29 30   // 还原方法31   public void decode(String str) {32     String st = "";33     int count = 0;34     String arr[] = str.split("_");35     for (int i = 0; i < arr.length; i++) {36       if (arr[i].length() == 1) {37         st += arr[i];38       } else if (arr[i].equals('\\' + "UL")) {39         st += "_";40       } else if (arr[i].length() > 1 && arr[i] != ('\\' + "UL")) {41         st += arr[i].length() - 1;42       } else if (arr[i].isEmpty()) {43         count++;44         if (!arr[i + 1].isEmpty()) {45           st = st + (count - 2);46         }47       }48     }49     System.out.println("还原后的字符串:" + st);50   }51 52   public static void main(String[] args) {53     Scanner sc1 = new Scanner(System.in);54     System.out.println("请输入要转换的字符串:");55     String str1 = sc1.nextLine();56     ZiFuChuan z1 = new ZiFuChuan();57     z1.encode(str1);58 59     Scanner sc2 = new Scanner(System.in);60     System.out.println("请输入要还原的字符串:");61     String str2 = sc2.nextLine();62     ZiFuChuan z2 = new ZiFuChuan();63     z2.decode(str2);64 65   }66 67 }

运行结果:

字符串测试题1

 

字符串测试题1

 

字符串测试题1




原标题:字符串测试题1

关键词:

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

菲律宾航空货运价格:https://www.goluckyvip.com/tag/96734.html
从中国寄菲律宾快运要多少钱:https://www.goluckyvip.com/tag/96735.html
海运菲律宾货运价格一览表:https://www.goluckyvip.com/tag/96736.html
货运菲律宾价格:https://www.goluckyvip.com/tag/96737.html
快递寄到菲律宾多少钱:https://www.goluckyvip.com/tag/96738.html
寄到菲律宾快递多少钱:https://www.goluckyvip.com/tag/96739.html
新喀里多尼亚/new caledonia:https://www.kjdsnews.com/a/1836448.html
美国对13家与俄罗斯相关的加密货币金融科技公司实施制裁:https://www.kjdsnews.com/a/1836449.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流