Question: C# Code to verify if a list starts with a prefix using only .Equals(), .CompareTo(), .count() in C# Example situations: list ={1,2,3,4}, prefix = {1,2}

C# Code to verify if a list starts with a prefix using only .Equals(), .CompareTo(), .count() in C#

Example situations:

list ={1,2,3,4}, prefix = {1,2}

list = {1,2,3,4), prefix = {1,2,3}

etc.

The problem with the code below is that it returns true after iterating though the first index of the list and prefix, without verifying the entire prefix is in the list.

for (int i=0, i<= prefix.Count(); i++)

if (prefix[i].Equals(list[i]))

return true;

return false;

How do I reformat this code so that it can check if the entire prefix is in list ie. prefix[0]=list[0] and then it goes into prefix[1]=list[1] etc. But does not need to go past the length of the prefix. Do not add different functions.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!