I need to write a method which takes in 2 arrays as parameters and returns true if the second array is a sub array of the first and false otherwise. I need to use only recursion without loops but I can use private methods.
So far this is what is have:
public static bool findSequence(char[] findIn, char[] toFind)
{
return compare(findIn, toFind, num);
}
private static int num = 0;
private static bool compare(char[] findIn, char[] toFind, int num)
{
for (int i = 0; i < findIn.Length; i++)
{
if (toFind[i] != findIn[num])
{
num++;
return false;
}
}
num++;
return true;
}
You are on the wrong way, since you have to use recursion and to avoid loops, and your code has loops and no recursion. I think that you should try a little bit harder, since this is very useful brain fitness. Anyway, this should work (and even understanding this could be a good exercise 🙂 ):