stringbuffer-keyboard input string, using StringBuffer's reverse () method to invert the display

Source: Internet
Author: User

Package CN. Stringbuffer;import Java.util.Scanner;/** Requirements: Keyboard input string, using StringBuffer's reverse () method to invert the display **/ Public classFanzhuan { Public Static voidMain (string[] args) {Scanner sc=NewScanner (System.inch); System. out. println ("Please input string"); String Str=Sc.nextline (); //String result = Fanzhuan (str); //String result = fanzhuan2 (str);String result =fanzhuan3 (str); System. out. println ("The result after reversal is:"+result); }    //string-"character array, get every character, then stitch     Public Staticstring Fanzhuan (String s) {string result=""; Char[] ch =S.tochararray ();  for(inti = ch.length-1; I >=0; i--) {result+=Ch[i]; }        returnresult; }    //Use the string charat to get each character, using length () to get the lengths     Public Staticstring fanzhuan2 (String s) {string result="";  for(inti = s.length ()-1; I >=0; i--) {result+=S.charat (i); }        returnresult; }    //direct inversion using StringBuffer's reverse () method     Public Staticstring Fanzhuan3 (string s) {/*StringBuffer buffer = new StringBuffer (s);        StringBuffer result = Buffer.reverse (); return result.tostring ();*/        return NewStringBuffer (s). Reverse (). toString (); }}

stringbuffer-keyboard input string, using StringBuffer's reverse () method to invert the display

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.