Compare two sentences

Source: Internet
Author: User

The purpose of the program is to compare two sentences. If there are different words in the two sentences, mark them.

Train of Thought: Call the split method of string to split the characters, compare them one by one, create a dyeing array, and record the results.

Equals method:

Equals is used to determine whether the strings are the same. First, it determines whether the references of the two objects are the same. If the strings are the same, it does not need to be determined. If they are different, it is necessary to determine whether the content of the strings is the same.

Function parameter passing in Java:

See: http://blog.csdn.net/niuniu20008/article/details/2953785


Result:



Code:

Import java. Io. *; public class test {public static void main (string [] ARGs) throws exception {int color []; string S1 = "I love you! "; System. out. print ("Enter the character:"); bufferedreader BR = new bufferedreader (New inputstreamreader (system. in); string S2 = BR. readline (); // by default, all values are 0 color = new int [s1.split (""). length]; compare (S1, S2, color); For (INT I = 0; I <color. length; I ++) {system. out. println ("A" + I + color [I]) ;}} Private Static void compare (string str1, string str2, int []) {string [] array1 = str1.split (""); string [] array2 = str2.split (""); If (array1.leng Th! = Array2.length) return; else {for (INT I = 0; I <array1.length; I ++) {If (array1 [I]. equals (array2 [I]) {A [I] = 1; system. out. println (I + "equal ");}}}}}


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.