Test whether the input string is a return text or a string return text.

Source: Internet
Author: User

Test whether the input string is a return text or a string return text.

If all characters except letters and numbers are removed from a string and both forward and reverse reads are the same, the string is a return. For example, "did Anna say as Anna did ?" It is a reply.

Programming: It is required to test whether the input string is a return text.

1 class Palindrome {2 3 public static void main (String [] args) {4 5 seconds SC = new seconds (System. in); 6 7 System. out. println ("please input a string:"); 8 9 String strInput = SC. nextLine (); 10 11 if (isPalind (strInput) {12 13 System. out. println ("\" "+ strInput +" \ "" + "is a reply! "); 14 15} else {16 17 System. out. println (" \ "" + strInput + "\" "+" is not a return! "); 18 19} 20 21} 22 23 public static boolean isPalind (String s) {24 25 String s1 = filter (s ); // filter out the characters 26 27 String s2 = reverse (s1) out of letters and numbers in s; // reverse the s1 String to 28 29 retrun s1.equals (s2 ); 30 31} 32 33 public static String filter (String s) {34 35 // complete the method 36 37 38} 39 40 public static String reverse (String s) {41 42 // complete method Supplement 43 44} 45 46} 47 48
View Code

The solution is as follows:

1 package zy. pg. ex3; 2 3 import java. util. *; 4 5 public class Experiment_3_Palindrome {6 7 public static void main (String [] args) {8 // TODO Auto-generated method stub 9 runtime SC = new Runtime (System. in); 10 System. out. println ("please input a string:"); 11 String strInput = SC. nextLine (); 12 if (isPalind (strInput) {13 System. out. println ("\" "+ strInput +" \ "" + "is a reply! "); 14} else {15 System. out. println (" \ "" + strInput + "\" "+" is not a return! "); 16} 17 18} 19 20 private static boolean isPalind (String s) {21 // TODO Auto-generated method stub22 String s1 = filter (s ); // filter out characters 23 String s2 = reverse (s1) out of letters and numbers in s; // reverse the s1 String to 24 return s1.equals (s2 ); 25 26} 27 public static String filter (String s) {28 // complete the method 29 StringBuffer f = new StringBuffer (); 30 char [] s1 = s. toCharArray (); 31 int flag = 0; 32 for (int I = 0; I <s1.length; I ++) {33 if (s1 [I] <= '9' & s1 [I]> = '0 ') | (s1 [I]> = 'A' & s1 [I] <= 'Z ') | (s1 [I]> = 'A' & s1 [I] <= 'Z') 34 f. insert (flag ++, s1 [I]); 35} 36 String news = new String (f); 37 return news; 38} 39 public static String reverse (String s) {40 // complete the method. 41 return new StringBuilder (s ). reverse (). toString (); 42} 43}
View Code

The first method of filter is to first identify whether a string can be created, and then link the filtered characters one by one behind the string to obtain the filtered string. However, I found on the Internet that JAVA actually has the string variable data type, and there is a method for connecting characters. It's no good.

The Declaration format is the same as other data types, but I learned a new declaration type.

1 String s;  2 String b;  3 StringBuffer sb = new StringBuffer(b);  4 sb.insert(0,s); 

This is an online search example. The first parameter of the insert method is the starting position of the insert position. The second parameter is the string to be inserted, but I am using a character. I guess the change is to treat the character as a string.

In addition, we learned how to convert string constants and string variables. The above code converts a String constant to a string variable. The opposite is similar.

I will name this transformation method a constructor Transformation Method for the moment. (Hey, maybe I have already heard this)

Then

That is, the string is put upside down. A method is required.

1 public static String reverse(String str){  2         return new StringBuilder(str).reverse().toString();  3 }

This is also seen on the Internet.

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.