Get the App
SLTechnology News&Howtos  ›  Development  › 

How does java judge that a string is symmetrical before and after

Shulou Source: shulou.com Published: 2022-06-01 05:14:46 10月02日 Update

This article will explain in detail how java judges that a string is symmetrical before and after. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

How to tell if a string is symmetrical?

What do you mean? It's like a string with a 90% discount before and after, it's symmetrical. It's like standing in front of the mirror and seeing a Yushu facing the wind and shyness of the moon.

Public class PalindromeString {public static void main (String [] args) {checkPalindromeString ("Silent King II"); checkPalindromeString ("Silent King II Silence");} private static void checkPalindromeString (String input) {boolean result = true; int length = input.length (); for (int I = 0; I < length / 2) ) {if (input.charAt (I)! = input.charAt (length-I-1)) {result = false; break;}} System.out.println (input + "is it symmetric? "+ result);}}

The output is as follows:

Is the Silence King symmetrical? False Silence Wang er er Wang Mo Shen symmetrical? True

Let's talk about my idea: to determine whether a string is symmetrical or not, it's easy to split it from the middle, compare the first character to the last character, and return false once you find the one that is not equal.

Pay attention to three points:

1) the subscript of the for loop starts at 0 and ends with length/2.

2) the subscript I and length-i-1 are symmetrical.

3) once false, break.

This is the end of this article on "how java determines that a string is symmetrical". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Symmetry character string article subscript more two kings good practical closed moon shy flower Yushu facing the wind middle content train of thought meaning article knowledge result mirror Yushu Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Xiaomi NVidia MySQL Docker