需求:
_ abc_ _ _df_ _hgsdfsdi_;
输出:
_hgsdfsdi_ _df_ _ _ abc_;
说明:
_ 代表是空格;
package test; import java.util.LinkedList; public class StringSortTest { /* * 输出 hgi df abc */ public static void main(String[] args) { String s = " abc df hgsdfsdi "; System.out.println(verseStr(s)); } public static String verseStr(String str) { StringBuffer sb = new StringBuffer(); LinkedList<Character> ll = new LinkedList<>(); for(int i=str.length()-1;i>=0;) { while(str.charAt(i) != ' ') { ll.addFirst(str.charAt(i)); i--; } int listSize = ll.size(); for(int j=0;j<listSize;j++) { sb.append(ll.pop()); } sb.append(str.charAt(i)); i--; } return sb.toString(); } }