-3

.For example if i enter two strings ,say input1="sum" and input 2="su". I should get an output saying 'm' is the extra character present in a string.

public class odd {
public static void main(String[] args)
{
  boolean b = false;
  ArrayList<String> list= new ArrayList<String>();
  ArrayList<String> list1= new ArrayList<String>();
  String n1="sum";
  String n2="su";
  for(int i=1;i<n1.length();i++){
    for(int j=1;j<n2.length();j++){
        if(n1.charAt(i)==n2.charAt(j)){
             b=true;
            break;
        } 
        else{
            System.out.println("Extra character is "+n1.charAt(i));
        }
  }
}

}
}
  • 6
    Possible duplicate of [How to perform string Diffs in Java?](http://stackoverflow.com/questions/132478/how-to-perform-string-diffs-in-java) – Pablo Matias Gomez Nov 29 '16 at 15:22
  • Ok, thanks for sharing your homework with us. What have you tried so far? Have you made any effort whatsoever? – tnw Nov 29 '16 at 15:24
  • Add the code you have tried so far, what doesnt work? How is your thought process as to solving this problem? – Alex Karlsson Nov 29 '16 at 15:25
  • @AlexKarlsson please check out the above code..it works partially. – Sandeep Raj Urs Nov 29 '16 at 15:31
  • @tnw please check the above code. – Sandeep Raj Urs Nov 29 '16 at 15:31
  • 1
    What's the actual problem? That's nice you have some code but you haven't told us what's wrong with it. Doesn't compile? If so, what's the compile error? Or does it compile but you encounter an exception? What's the exception? Maybe the output is wrong? BE MORE SPECIFIC AND DETAILED. – tnw Nov 29 '16 at 16:17

1 Answers1

0

One easy way you can do it if the only condition is to find the extra character added, is to use split. So in your case it would look something like this:

String string1 = "sum";
String string2 = "su";
String[] array1 = string1.split(string2);
System.out.println(array1[1]);

This will print 'm'. And then you have to add conditions if you want to check which is the longer string etc. But this is a start.

Pablo Matias Gomez
  • 6,614
  • 7
  • 38
  • 72
Alex Karlsson
  • 296
  • 1
  • 11