Saturday, 24 November 2018

Comparing two strings in java character by character

public class StringEx {
public static void main(String[] args) {
String s1 = "listen";
String s2 = "SILENCE";
char[] first = s1.toLowerCase().toCharArray();
char[] second = s2.toLowerCase().toCharArray();
int minLength = Math.min(first.length, second.length);
System.out.println(minLength);
for (int i = 0; i < minLength; i++) {
if (first[i] != second[i]) {

System.out.println("false");
} else {
System.out.println("True");
}
}
}
}

Output:
===========
false
True
false
false
false
false
false

No comments:

Post a Comment