Implement a recursive function that tests whether two strings are equal. static
ID: 3673296 • Letter: I
Question
Explanation / Answer
Answer:
Implementation of recursive function that tests whether two strings are equal :
public static boolean areEqual(String one, String two) {
if(one.length()==0 && two.length()==0) return true;
if(one.length()==0 )
{
if(two.charAt(0)!='*')
return false;
if(two.length()!=1)
return areEqual(one,two.substring(1));
else
return true;
}
if(two.length()==0 )
{
if(one.charAt(0)!='*')
return false;
if(one.length()!=1)
return areEqual(two,one.substring(1));
else
return true;
}
if(one.equals(two) || one.equals("*") || two.equals("*"))
{
return true;
}
if(one.charAt(0)=='@' || two.charAt(0)=='@' ||one.charAt(0)==two.charAt(0))
{
if(one.length()==1 && two.length()==1) return true;
if(one.length()>1 && two.length()>1)
{
return (areEqual(one.substring(1),two.substring(1)));
}
}
if(one.charAt(0)=='*')
{
if(areEqual(one.substring(1),two)==true)
{
return true;
}
else if (areEqual(one.substring(1),two.substring(1))==true)
{
return true;
}
else
{
return (areEqual(one, two.substring(1)));
}
}
if(two.charAt(0)=='*')
{
if(areEqual(two.substring(1),one)==true)
{
return true;
}
else if (areEqual(one.substring(1),two.substring(1))==true)
{
return true;
}
else
{
return (areEqual(two, one.substring(1)));
}
}
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.