1. Analysis: Judging whether a string is a symmetric string, we just need to put the string of the 1th character and the last 1 characters, the 2nd character and the countdown 2nd character, ...
The number of comparisons is the length divided by 2.
2. Case Demo:
1 Packagecn.itcast_07;2 3 ImportJava.util.Scanner;4 5 /*6 * Determine if a string is a symmetric string7 * For example, "ABC" is not a symmetric string, "ABA", "ABBA", "AAA", "MNANM" are symmetric strings8 * 9 * Analysis:Ten * Judging If a string is a symmetric string, I just need to put One * First and last comparison A * Second and penultimate comparison - * ... - * The number of comparisons is the length divided by 2. the */ - Public classStringBufferTest4 { - Public Static voidMain (string[] args) { - //Creating a Keyboard entry object +Scanner sc =NewScanner (system.in); -System.out.println ("Please enter a string:"); +String s =sc.nextline (); A at //A comparison of one - Booleanb =Issame (s); -System.out.println ("B:" +b); - - //inverse function with string buffers - BooleanB2 =isSame2 (s); inSystem.out.println ("B2:" +B2); - } to + Public Static BooleanisSame2 (String s) { - return NewStringBuffer (s). Reverse (). ToString (). Equals (s); the } * $ Panax Notoginseng //Public Static Boolean issame (String s) { - // //turn a string into a character array the //char[] CHS = S.tochararray (); + // A //For (int start = 0, end = chs.length-1; start <= end; start++, end--) { the //if (Chs[start]! = Chs[end]) { + //return false; - // } $ // } $ // - //return true; - // } the - Public Static BooleanIssame (String s) {Wuyi BooleanFlag =true; the - //turn a string into a character array Wu Char[] CHS =S.tochararray (); - About for(intStart = 0, end = chs.length-1; Start <= end; start++, end--) { $ if(Chs[start]! =Chs[end]) { -Flag =false; - Break; - } A } + the returnFlag; - } $}
The running results of the program are as follows:
Java Fundamentals Hardening the judgment of 47:stringbuffer class whether a string is symmetric case