Java反转字母文本
2021/11/13 20:42:18
本文主要是介绍Java反转字母文本,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
给定一个英语文本 ,将英语文本反转 例:When you were born,you were crying and everyone around you was smiling.Live your life so that when you die,you're the one who is smilling and everyone around you is crying. 输出:.crying is you around everyone and smilling is who one the re'you,die you when that so life your Live.smiling was you around everyone and crying were you,born were you When public static void main(String[] args) { String s ="When you were born,you were crying and everyone around you was smiling.Live your life so that when you die,you're the one who is smilling and everyone around you is crying."; StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(s); //在符号两端加空格 for (int i = 0; i < stringBuffer.length(); i++) { if (!Character.isLetterOrDigit(stringBuffer.charAt(i)) && stringBuffer.charAt(i)!=' ') { stringBuffer.insert(i," "); stringBuffer.insert(i+2," "); i+=2; } } String s1 = stringBuffer.toString(); StringBuffer stringBuffer1 = new StringBuffer(); //将空格变成数组 String[] s2 = s1.split(" "); for (int i = s2.length - 1; i >=0 ; i--) { if (i!=0 && i != s2.length - 1){ stringBuffer1.append(s2[i]+" "); }else { stringBuffer1.append(s2[i]); } } //将符号两端空格去除 int length = stringBuffer1.length(); for (int i = 0; i < length ; i++) { if (i!=0 && i!= stringBuffer1.length()-1 && !Character.isLetterOrDigit(stringBuffer1.charAt(i)) && stringBuffer1.charAt(i)!=' '){ stringBuffer1.deleteCharAt(i-1); stringBuffer1.deleteCharAt(i); length = length-2; } } System.out.println(stringBuffer1.toString()); } }
这篇关于Java反转字母文本的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南