web123456

Use StringBuilder to complete string inversion (very classic)

package MyString; import java.util.Scanner; /* Use StringBuilder to complete string inversion Analysis: 1. Enter a string on the keyboard 2. Write method to implement string inversion --toString converted to String 3. Calling methods 4. Output result */ public class StringTest02 { public static void main(String[] args) { System.out.println("Please enter a string:"); Scanner sc = new Scanner(System.in); String s = sc.nextLine(); String s1 = StringReverse(s); System.out.println("S1 after reversal:"+s1); } /* Two clear: Return value type: String Parameter list: String */ public static String StringReverse(String s){ StringBuilder sb = new StringBuilder(s); sb.reverse(); String s1 = sb.toString(); return s1; } }