Sandeep Raj Urs Sandeep Raj Urs - 11 days ago 6
Java Question

Can anyone help me to get extra character by comparing 2 inputed strings by using java

.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));
}
}
}

}
}

Answer

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.