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?
1
Expert's answer
2012-11-27T06:31:28-0500
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));
}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS