字符串反转 字符串中的单词逆序输出 java

/**字符串反转
 * 
*如,输入“I love China",要求输出"China love I"
*/

import java.util.Scanner;

public class SwapWord
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter a string, such as 'I love China'");
		String s = new String(sc.nextLine());
		System.out.println("the string that was swaped before was:");
		System.out.println(s);
		System.out.println("the swaped string:");
		System.out.println(swapWord(s));
	}
	
	/**字符串中的单词从后到前排列
	 * 先把字符串全部反转,在按空格分隔来反转各个单词*/
	public static String swapWord(String s)
	{
		char[] a = s.toCharArray();
		swap(a,0,a.length-1);//现在得到的是“anihC evol I”
		
		int blank = -1;//前一个空格的位置
		for(int i=0;i

你可能感兴趣的:(编程基础,java,字符串)