Skip to content

Instantly share code, notes, and snippets.

@thmain
Created June 2, 2018 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/18775441e4bb1d94284ea492aafd8a93 to your computer and use it in GitHub Desktop.
Save thmain/18775441e4bb1d94284ea492aafd8a93 to your computer and use it in GitHub Desktop.
public class CompareStrings {
public static boolean compare(String x, String y){
if(x==null || y==null){
return false;
}
//compare lengths
if(x.length()!=y.length())
return false;
//compare all characters
for (int i = 0; i <x.length() ; i++) {
if(x.charAt(i)!=y.charAt(i))
return false;
}
//if here, means both strings are equal
return true;
}
public static void main(String[] args) {
String x = "tutorial";
String y = "tutorial";
System.out.println("String x='" + x + "' and String y='" + y + "' are equal?? -" + compare(x, y));
x = "tutorial";
y = "tutorial ";
System.out.println("String x='" + x + "' and String y='" + y + "' are equal?? -" + compare(x, y));
x = "tutorial";
y = " ";
System.out.println("String x='" + x + "' and String y='" + y + "' are equal?? -" + compare(x, y));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment