Answer to Question #19556 in Java | JSP | JSF for Alex
Question #19556
How can I compare two strings a character at a time and have it return string 1 if more than one letter is different?
Expert's answer
public class Test {
public static String check(String one, String two) {
& boolean equals = false;
& for (int i = 0; i < one.length(); i++) {
& if (one.charAt(i) != two.charAt(i)) {
& equals = false;
& } else
& equals = true;
& }
& if (!equals)
& return one;
& else
& return "";
}
public static void main(String[] args) {
& String one = "string one";
& String two = "string two";
& System.out.print(check(one, two));
}
}
public static String check(String one, String two) {
& boolean equals = false;
& for (int i = 0; i < one.length(); i++) {
& if (one.charAt(i) != two.charAt(i)) {
& equals = false;
& } else
& equals = true;
& }
& if (!equals)
& return one;
& else
& return "";
}
public static void main(String[] args) {
& String one = "string one";
& String two = "string two";
& System.out.print(check(one, two));
}
}
Need a fast expert's response?
Submit orderand get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Comments
Leave a comment