2

I am trying to check whether the position in the array where the two strings are different. For example if I have the String apple and the string appendix then the two string are different at the position where i = 3.

How can I check that with Java?

Code

 //The first string is s.
    char[] cArray = s.toCharArray();
    // The seond string is root.edge.
    char[] rootEdgeCharacter = root.edge.toCharArray();

    for(int i=0; i<cArray.length; i++  ){
        for(int j=0; j<rootEdgeCharacter.length; j++){
            if(cArray[i]== rootEdgeCharacter[j]){
                System.out.println("The String are different where i =" + i);

            }
        }
    }   
2
  • 1
    What is your question? Commented Nov 18, 2015 at 15:42
  • I understand what you are trying to do.... but that code is definitely not right. I hope this helps Commented Nov 18, 2015 at 15:42

2 Answers 2

5

Don't use .toCharArray(), it needlessly creates a new character array. Use .charAt() instead.

What is more, your code will not walk arrays "in parallel": you iterate at indices 0, 0 then 1, 0, then 3, 0 etc. This is not what you want.

Here is one solution; note that for string of inequal length it will return the smaller length, and it both strings are equal it returns -1:

public static int findDifferingIndex(final String s1, final String s2)
{
    final int len1 = s1.length();
    final int len2 = s2.length();
    final int len = Math.min(len1, len2);

    for (int index = 0; index < len; index++)
        if (s1.charAt(index) != s2.charAt(index))
            return index;

    // Check the length of both strings; if they are equal, return -1.
    // Otherwise return the length `len`.
    return len1 == len2 ? -1 : len;
}
Sign up to request clarification or add additional context in comments.

5 Comments

My little adding: add this checking: if(s1.equals(s2)){ return -1; } This way you save computing time in case they both were equals. Not null checking is optative and, finally, don't see sense on adding "final" attribute to input parameters
@jmarserr if you .equals() you check all characters twice; as to final on parameters, well, it tells the user that those will never be modified in the method itself
It would save computation time in case strings were different skipping the most costly part of method but It's programmer's duty to decide if this saving compensate the possibility of this double checking. About the use of "final", what i really meant to say is that i didn't see the point in protect this way these input parameters knowing that they are Strings (so changes inside of the method won't affect outside) and the method aim is not to modify any of them. Would you protect with final modifiers all input parameters in all your methods?
@jmarserr 1. I don't see that it would save any computation time given the OP's requirement, quite the opposite in fact. If the strings are not equal, you'd then have to walk character by character anyway. 2. it is not because the class String is final that you cannot reaffect a parameter to a method in code: void foo(String s) { s = "meh"; }. While this looks like stupid code, you can do it, that is reuse method parameters inside the method. Making parameters final prevents such {re,ab}use.
Imagine two 50000 length strings and the effort used comparing them if we followed existing code in case they were equals; wouldn't it be best option to use equals at the beginning and avoid that? Anyway, no discussion, this was a little adding and it's programmer duty to know what are his needs and decide what to use.About your use of "final", would it mean its use in every input parameter of every method as a normal rule? and when not?. Once more, design decision (there are online discussions about this issue online if interested)
1

What you can do is loop through the array and get 2 strings at a time.

Check the difference betweent these using the method getDiff.

The Method looks for the characaters untill a mis-match is found.

a p p l e
a p p l y
        ^

It check character by character untill a mis-match is found.

String [] arr = {"apple", "apply", "application", "apraisal"};

for(int i = 0; i < arr.length; ++i)
{
 for(int j = i + 1; j < arr.length; ++j)
  {
    System.out.println(arr[i] + " " + arr[j] + " " + getDiff(arr[i], arr[j]));
  }
}

public static int getDiff(Stirng s1, String s2)
{
 for(int i = 0; i < Math.min(s1.length, s2.length); ++i)
  if(s1.charAt(i) != s2.charAt(i))
   return i;
 return -1;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.