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

Where in the c# code can i enter the information to print to console? Console.Wr

ID: 3913593 • Letter: W

Question

Where in the c# code can i enter the information to print to console?  

Console.WriteLine("Pattern found at:{0}", i);

Console.WriteLine(pattern)

public void preKMP()
{
int m = pattern.Length;
int k;
f[0] = -1;
for (int i = 1; i < m; i++)
{
k = f[i - 1];
while (k >= 0)
{
if (pattern[k] == pattern[i - 1])
break;
else
k = f[k];
}
f[i] = k + 1;
}
}


public int Search() // s is string sequence, pattern is what is inputted from user
{

//kmp algorithm is executed here

int m = pattern.Length;
int n = s.Length;
int[] f = new int[m];

preKMP();
int i = 0;
int k = 0;

while (i < n)
{
if (k == -1)
{
++i;
k = 0;
}
else if (s[i] == pattern[k])
{
i++;
k++;
if (k == m)
return 1;

  
}
else
k = f[k];

}
return 0;

  
}

Explanation / Answer

You can write the statements inside the search function.

Console.WriteLine("Pattern found at:{0}", i); can be used to print info the value of i when search found

Console.WriteLine(pattern) can be used to print the pattern after the search found.

below inserted into the code:

public void preKMP()
{
int m = pattern.Length;
int k;
f[0] = -1;
for (int i = 1; i < m; i++)
{
k = f[i - 1];
while (k >= 0)
{
if (pattern[k] == pattern[i - 1])
break;
else
k = f[k];
}
f[i] = k + 1;
}
}


public int Search() // s is string sequence, pattern is what is inputted from user
{

//kmp algorithm is executed here
//wanted to print the pattern before searching
Console.WriteLine(pattern);
int m = pattern.Length;
int n = s.Length;
int[] f = new int[m];

preKMP();
int i = 0;
int k = 0;

while (i < n)
{
if (k == -1)
{
++i;
k = 0;
}
else if (s[i] == pattern[k])
{
i++;
k++;
if (k == m)
{
//print the value of i when search found
Console.WriteLine("Pattern found at:{0}", i);

Console.WriteLine(pattern);
return 1;
}
  
}
else
k = f[k];

}
return 0;

  
}

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