Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a recursive function that tests whether two strings are equal. static

ID: 3673296 • Letter: I

Question


Implement a recursive function that tests whether two strings are equal. static boolean areEqual(String one, String two) {... } You can use the following String member functions: class String { char char At(int index);//Returns the char value at the specified index. int length()://Returns the length of a string String substring (int beginIndex);//Returns a new string that is a substring of this string. String substring (int beginIndex, int endlndex);//Returns a new siring that is a substring of this string. }

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote