Algorithm


  1. Input Parameters:

    • str1: First input string
    • str2: Second input string
  2. Variable Initialization:

    • l1: Length of str1
    • l2: Length of str2
    • lmin: Minimum length among l1 and l2
  3. Comparison Loop:

    • Iterate from i = 0 to lmin - 1
      • Get the ASCII value of the i-th character of str1 and str2 as str1_ch and str2_ch
      • Compare str1_ch and str2_ch
        • If they are not equal, return the difference str1_ch - str2_ch
  4. Edge Case Check:

    • If the loop completes without returning, check if l1 is not equal to l2
      • If true, return the difference l1 - l2 (based on lengths)
  5. Default Case:

    • If none of the above conditions is true, both strings are equal, return 0.

Code Examples

#1 Example- user-defined function

Code - Java Programming

// Java program to Compare two strings 
// lexicographically 
public class DVE{ 

	// This method compares two strings 
	// lexicographically without using 
	// library functions 
	public static int stringCompare(String str1, String str2) 
	{ 

		int l1 = str1.length(); 
		int l2 = str2.length(); 
		int lmin = Math.min(l1, l2); 

		for (int i = 0; i  <  lmin; i++) { 
			int str1_ch = (int)str1.charAt(i); 
			int str2_ch = (int)str2.charAt(i); 

			if (str1_ch != str2_ch) { 
				return str1_ch - str2_ch; 
			} 
		} 

		// Edge case for strings like 
		// String 1="Devs" and String 2="DevsEnv" 
		if (l1 != l2) { 
			return l1 - l2; 
		} 

		// If none of the above conditions is true, 
		// it implies both the strings are equal 
		else { 
			return 0; 
		} 
	} 

	// Driver function to test the above program 
	public static void main(String args[]) 
	{ 
		String string1 = new String("DevsEnv"); 
		String string2 = new String("Practice"); 
		String string3 = new String("Devs"); 
		String string4 = new String("Devs"); 

		// Comparing for String 1  <  String 2 
		System.out.println("Comparing " + string1 + " and " + string2 
						+ " : " + stringCompare(string1, string2)); 

		// Comparing for String 3 = String 4 
		System.out.println("Comparing " + string3 + " and " + string4 
						+ " : " + stringCompare(string3, string4)); 

		// Comparing for String 1 > String 4 
		System.out.println("Comparing " + string1 + " and " + string4 
						+ " : " + stringCompare(string1, string4)); 
	} 
} 
Copy The Code & Try With Live Editor

Output

x
+
cmd
Comparing DevsEnv and Practice : -9
Comparing Devs and Devs: 0
Comparing DevsEnv and Devs: 8

#2 Code Example Using String.equals()

Code - Java Programming

// Java program to Compare two strings 
// lexicographically 
public class DVE{ 
	public static void main(String args[]) 
	{ 
		String string1 = new String("DevsEnv"); 
		String string2 = new String("Practice"); 
		String string3 = new String("Devs"); 
		String string4 = new String("Devs"); 
		String string5 = new String("Devs"); 

		// Comparing for String 1 != String 2 
		System.out.println("Comparing " + string1 + " and " + string2 
						+ " : " + string1.equals(string2)); 

		// Comparing for String 3 = String 4 
		System.out.println("Comparing " + string3 + " and " + string4 
						+ " : " + string3.equals(string4)); 

		// Comparing for String 4 != String 5 
		System.out.println("Comparing " + string4 + " and " + string5 
						+ " : " + string4.equals(string5)); 

		// Comparing for String 1 != String 4 
		System.out.println("Comparing " + string1 + " and " + string4 
						+ " : " + string1.equals(string4)); 
	} 
} 
Copy The Code & Try With Live Editor

Output

x
+
cmd
Comparing DevsEnv and Practice : false
Comparing Devs and Devs: true
Comparing Devs and devs: false
Comparing DevsEnv and Devs: false

#3 Code Example with Using String.equalsIgnoreCase()

Code - Java Programming

// Java program to Compare two strings 
// lexicographically 
public class DVS{ 
	public static void main(String args[]) 
	{ 
		String string1 = new String("DevsEnv"); 
		String string2 = new String("Practice"); 
		String string3 = new String("Devs"); 
		String string4 = new String("Devs"); 
		String string5 = new String("devs"); 

		// Comparing for String 1 != String 2 
		System.out.println("Comparing " + string1 + " and " + string2 
						+ " : " + string1.equalsIgnoreCase(string2)); 

		// Comparing for String 3 = String 4 
		System.out.println("Comparing " + string3 + " and " + string4 
						+ " : " + string3.equalsIgnoreCase(string4)); 

		// Comparing for String 4 = String 5 
		System.out.println("Comparing " + string4 + " and " + string5 
						+ " : " + string4.equalsIgnoreCase(string5)); 

		// Comparing for String 1 != String 4 
		System.out.println("Comparing " + string1 + " and " + string4 
						+ " : " + string1.equalsIgnoreCase(string4)); 
	} 
} 
Copy The Code & Try With Live Editor

Output

x
+
cmd
Comparing DevsEnvand Practice : false
Comparing Devs and Devs: true
Comparing Devs and Devs: true
Comparing DevsEnv and Devs: false

#4 Example: Using Objects.equals()

Code - Java Programming

// Java program to Compare two strings 
// lexicographically 

import java.util.*; 

public class DVS{ 
	public static void main(String args[]) 
	{ 
		String string1 = new String("DevsEnv"); 
		String string2 = new String("Devs"); 
		String string3 = new String("Devs"); 
		String string4 = null; 
		String string5 = null; 

		// Comparing for String 1 != String 2 
		System.out.println("Comparing " + string1 + " and " + string2 
						+ " : " + Objects.equals(string1, string2)); 

		// Comparing for String 2 = String 3 
		System.out.println("Comparing " + string2 + " and " + string3 
						+ " : " + Objects.equals(string2, string3)); 

		// Comparing for String 1 != String 4 
		System.out.println("Comparing " + string1 + " and " + string4 
						+ " : " + Objects.equals(string1, string4)); 

		// Comparing for String 4 = String 5 
		System.out.println("Comparing " + string4 + " and " + string5 
						+ " : " + Objects.equals(string4, string5)); 
	} 
} 
Copy The Code & Try With Live Editor

Output

x
+
cmd
Comparing DevsEnv and Devs: false
Comparing Devs and Devs: true
Comparing DevsEnv and null : false
Comparing null and null : true
Advertisements

Demonstration


Java Programming to Compare Two String-DevsEnv