Algorithm
-
Input Parameters:
str1
: First input stringstr2
: Second input string
-
Variable Initialization:
l1
: Length ofstr1
l2
: Length ofstr2
lmin
: Minimum length amongl1
andl2
-
Comparison Loop:
- Iterate from
i = 0
tolmin - 1
- Get the ASCII value of the
i
-th character ofstr1
andstr2
asstr1_ch
andstr2_ch
- Compare
str1_ch
andstr2_ch
- If they are not equal, return the difference
str1_ch - str2_ch
- If they are not equal, return the difference
- Get the ASCII value of the
- Iterate from
-
Edge Case Check:
- If the loop completes without returning, check if
l1
is not equal tol2
- If true, return the difference
l1 - l2
(based on lengths)
- If true, return the difference
- If the loop completes without returning, check if
-
Default Case:
- If none of the above conditions is true, both strings are equal, return
0
.
- If none of the above conditions is true, both strings are equal, return
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
Comparing DevsEnv and Practice : -9
Comparing Devs and Devs: 0
Comparing DevsEnv and Devs: 8
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
Comparing DevsEnv and Practice : false
Comparing Devs and Devs: true
Comparing Devs and devs: false
Comparing DevsEnv and Devs: 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
Comparing DevsEnvand Practice : false
Comparing Devs and Devs: true
Comparing Devs and Devs: true
Comparing DevsEnv and Devs: 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
Comparing DevsEnv and Devs: false
Comparing Devs and Devs: true
Comparing DevsEnv and null : false
Comparing null and null : true
Comparing Devs and Devs: true
Comparing DevsEnv and null : false
Comparing null and null : true
Demonstration
Java Programming to Compare Two String-DevsEnv