Java Fundamentals Hardening the judgment of 47:stringbuffer class whether a string is symmetric case

Source: Internet
Author: User

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

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.