java 求一个字符串的倒序输出(world the Hello)

package com.uj.str;

import java.io.PrintStream;
import java.util.Arrays;

public class ReverseString {

    public static void main(String[] args) {

        System.out.println("Target : " + reverseString(" Language is a beautiful! "));
    }

    public static String reverseString(String str) {
        String[] splidStr = reverse(str).split(" ");
        StringBuffer sb = new StringBuffer();
        for (String string : splidStr) {
            sb.append(reverse(string)+" ");
        }
        return sb.toString();
    }
        public static String reverse(String str) {
        char[] c = str.toCharArray();
        int length = c.length;
        for (int i = 0; i < c.length / 2; i++) {
            char t = c[length-1-i];
            c[length-1-i] = c[i];
            c[i] = t;
        }
        String  target = new String(c);
        return target;

    }

}

你可能感兴趣的:(算法)