Reverse sentence, with input and output

Source: Internet
Author: User


An English statement is emitted in reverse order from the word unit. For example, "I am a boy", in reverse order after the discharge of "Boy a AM I"
All words are separated by a space with no other characters in the statement except the English letters


Interface description

/**
* Inverted sentence
*
* @param sentence The original sentence
* @return The inverted sentence
*/
public string Reverse (string sentence);


Enter a description:

An English statement is emitted in reverse order from the word unit.

Output Description:

Get a sentence in reverse order
Enter an example:

I am a Boy

Output Example:
Boy a AM I

Import Java.util.Scanner;

public class main{public
	void Swap (char [] arr,int Front,int end) {while
		(front<end) {
			char tmp;
			Tmp=arr[front];
			Arr[front]=arr[end];
			arr[end]=tmp;
			front++;
			end-- 
		}
	}
	public string Swapwords (String str) {
		
		char[] Chararry=str.tochararray ();
		int len=chararry.length;
		int begin=0;
		
		Swap (Chararry, 0, len-1);
	
		for (int i = 0;i < len;i++) {
			if (chararry[i]== ') {
				swap (Chararry, begin, i-1);
				Begin=i+1		
			}
		}
		Swap (Chararry, begin, len-1);
		return new String (Chararry);
		
		
	}
	

	 

	public static void Main (string[] args) {
		//TODO auto-generated method stub
		StringBuilder string=new Stringbuil Der ();
		Scanner Scanner =new  Scanner (system.in);
		while (Scanner.hasnext ()) {
			string.append (Scanner.nextline ());
			 
			System.out.println (New Main (). Swapwords (new String));}}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.