1

I have two strings :

String s1 = "D3516025204        StackOverflow is good.";
String s2 = "D3516025204        StackOverflow Nice to see you.";

First compare both the string character by character and print the indexes or column no. if the characters are different. I need to include the spaces in the string.

Like in above string :

Difference of indexes from s1 and s2: 34 to 48.

I tried below :

        char[] first = text1.toLowerCase().toCharArray();

        char[] second = text2.toLowerCase().toCharArray();

        int minimum = Math.min(first.length, second.length);

        StringBuffer buffer = new StringBuffer();

        for (int i = 0; i < minimum; i++) {

            if (first[i] != second[i]) {
                do{
                //System.out.print(first[i]+"   "+second[i]);
                System.out.println(text1.offsetByCodePoints(first[i],text1.indexOf(second[i])));

            }while(first[i] == second[i]);
        }

        }
rockersdeal
  • 71
  • 12
  • First, I see an infinite loop with your do{}while. You never increment i in this loop. Second, why did you write an if to do nothing in it ? Finally, what is exactly the condition for the diffrence, is it always s1 compare to s2 ? – AxelH May 06 '16 at 10:05
  • I need to compare both the strings i.e. s1 compare to s2. if there is difference in string then get the indexes i.e. from index - to index. – rockersdeal May 06 '16 at 10:09

4 Answers4

1

Take a look at apache.commons.lang3.StringUtils or this post.

Using StringUtils.difference(String first, String second) in your code could look something like this

public static void main(String[] args) {
    String s1 = "D3516025204        StackOverflow is good.";
    String s2 = "D3516025204        StackOverflow Nice to see you.";
    String s = StringUtils.difference( s1, s2);
    int at = StringUtils.indexOfDifference(s1, s2);
    System.out.println(at + "-" + s2.length());
    System.out.println(s);
}
Jasperan
  • 2,154
  • 1
  • 16
  • 40
Eritrean
  • 15,851
  • 3
  • 22
  • 28
1
    String text1 = "D8516025209        StackOverflow is good.";
    String text2 = "D3516025204        StackOverflow Nice to see you.";

    int minimum = Math.min(text1.length(), text2.length());
    int maximum = Math.max(text1.length(), text2.length());

    StringBuilder stringBuilder = new StringBuilder("Difference of indices from s1 and s2: ");
    StringBuilder text1Diff = new StringBuilder();
    StringBuilder text2Diff = new StringBuilder();
    int index = 0;

    while (index < minimum)
    {
        if (text1.charAt(index) != text2.charAt(index))
        {
            stringBuilder.append(stringBuilder.length() > 0 ? ", " : "").append(index + 1).append(" - ");
            text1Diff.append(text1Diff.length() > 0 ? ", " : "");
            text2Diff.append(text2Diff.length() > 0 ? ", " : "");

            while ((index < minimum) && (text1.charAt(index) != text2.charAt(index)))
            {
                text1Diff.append(text1.charAt(index));
                text2Diff.append(text2.charAt(index));
                index++;
            }

            if (index == minimum)
            {
                stringBuilder.append(maximum);
                index = maximum;

                if (maximum == text1.length())
                    text1Diff.append(text1.substring(minimum));
                else
                    text2Diff.append(text2.substring(minimum));
            }
            else
                stringBuilder.append(index);
        }
        index++;
    }

    if (minimum != maximum && index < maximum)
    {
        stringBuilder.append(stringBuilder.length() > 0 ? ", " : "").append(minimum + 1).append(" - ").append(maximum);

        if (maximum == text1.length())
            text1Diff.append(text1.substring(minimum));
        else
            text2Diff.append(text2.substring(minimum));
    }

    System.out.println(stringBuilder.toString());
    System.out.println("Differences: ");
    System.out.println(text1Diff.toString());
    System.out.println(text2Diff.toString());
aviad
  • 1,553
  • 12
  • 15
  • this somehow works for me but unfortunately i came to a situation that i have two files to compare which has multiple records around 50K in both the files. Now the sourceLine and targetLine can be different but sourceLine can be availlable in next line or to another next line. So, it means we cant differentiate the two lines line by line. But your solution is good enough, if the two lines are having same set of data but with minor changes. So +1 for you. – rockersdeal May 12 '16 at 15:17
  • I want to get the String of characters with difference also. I think i should use String.valueOf(char); Please update. – rockersdeal May 17 '16 at 07:26
  • which characters do you want? of text1 or text 2? or both? – aviad May 18 '16 at 10:00
0

try this.

    String s1 = "D3516025204        StackOverflow is good.";
    String s2 = "D3516025204        StackOverflow Nice to see you.";

    if(s1.length() > s2.length()){
        for(int i=0; i< s1.length() - s2.length(); i++)
            s2+=" ";
    }else{
        for(int i=0; i< s2.length() - s1.length(); i++)
            s1+=" ";
    }
    char[] c1 = s1.toCharArray();
    char[] c2 = s2.toCharArray();

    System.out.print("Difference is: ");
    for(int i=0; i<c1.length; i++){
        if(c1[i]!=c2[i]){
            System.out.print(i+", ");
        }
    }
Muhammad Suleman
  • 2,892
  • 2
  • 25
  • 33
  • why have you taken the hard code value in substring, it can be dynamic values. Bcoz i have multiple strings to compare of different lengths. Even it does not print the index it prints only values. – rockersdeal May 06 '16 at 10:12
  • I guess its not a very hard thing to change into any input data. but let me edit my answer – Muhammad Suleman May 06 '16 at 10:35
  • there is no infinite loop here, You just have to enter 2 numbers as inputs of start and end index then it will run. – Muhammad Suleman May 06 '16 at 10:57
  • Buts thats not the requirement. I don't know where is the difference, then why should i enter the difference of index. I need to identify the indexes of differences. – rockersdeal May 06 '16 at 11:01
0
class CrazySolution {
public static void main(String[] args) throws Exception {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String line = br.readLine();
    String line1 = br.readLine();
    //process()
    int lenofLine = line.length();
    int lenofLine1 = line1.length();
    int finalLen = Math.min(lenofLine, lenofLine1);

    for (int i = 0; i < finalLen; i++) {
        if (line.charAt(i) != line1.charAt(i))
           {
            System.out.println(i);
        break;
         }
    }
    System.out.println(lenofLine == lenofLine1 ? -1 : finalLen);

}

}

Try this..

Rishal
  • 1,480
  • 1
  • 11
  • 19