package Test;
import java.util.regex.Pattern;
public class T1 {
public static void main(String[] args) { System.out.println(reversalString("I am a student. 我 很厉害! 哈哈!!!")); System.out.println(reversalString("abcdefghijk",2,5)); }//I am a student. 我 很厉害! 哈哈!!!-->哈哈!!! 很厉害! 我 student. a am I
public static String reversalString(String str){ str = reversalString(str,0,str.length()-1); char[] cStr = str.toCharArray(); System.out.println("先把所有的都反转过来->"+str); int tag=-1; for(int i=0;i<cStr.length;i++){//如果碰见空格,就把两个空格直接的元素进行转换 if(Pattern.compile("\\s").matcher(String.valueOf(cStr[i])).find()){ str = reversalString(str,tag+1,i-1); tag = i; System.out.println("空格出现的位置->"+i); } } return str; } //字符串在指定位置反转 :abcdefghijk-->abfedcghijk public static String reversalString(String str,int start,int end){ char[] carr = str.toCharArray(); for(int i=0;i<=(end-start)/2;i++){ char temp; temp = carr[start+i]; carr[start+i] = carr[end-i]; carr[end-i] = temp; } return new String(carr); }}
递归实现字符串反向输出public static void print(){ Scanner scanner = new Scanner(System.in); String str = scanner.next(); if(!str.equals("#")){ print(); } if(!str.equals("#")){ System.out.println(str); } }public static void main(String[] args) { print(); }