Difference Between Two String

The following JAVA PROGRAM calculates the difference between two strings provided the initial contents in both the strings remain the same.

 

The logic behind this program is to consider the lengths of the two strings.

If the first string is having larger length, then it is divided to get the difference and same goes for the second string.

 

Here is the sample code snippet.

package samples;

public class DifferenceBetweenTwoStrings {

public static void main(String[] args) {

String one = “this is first string”;

String two = “this is first string with some extra content”;

if (one.length() > two.length()) {

System.out.println(one.substring(two.length() – 1));

} else if (two.length() > one.length()) {

System.out.println(two.substring(one.length() – 1));

} else {

System.out.println(“something went wrong”);

}

}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s