Question

Where in the c# code can i enter the information to print to console?   Console.WriteLine("Pattern found...

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;

  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;

  
}

Add a comment
Know the answer?
Add Answer to:
Where in the c# code can i enter the information to print to console?   Console.WriteLine("Pattern found...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • i need help fixing my code. here is the assignment: Make a class that contains the...

    i need help fixing my code. here is the assignment: Make a class that contains the following functions: isLong, isDouble, stringToLong, and stringToDouble. Each receives a string and returns the appropriate type (bool, bool, long, and double).During the rest of the semester you will paste this class into your programs and will no longer use any of the "standard" c# functions to convert strings to numbers. here is my code. it works for the long method but not the double:...

  • C#, I am getting error placing them all in one file pls set them in the...

    C#, I am getting error placing them all in one file pls set them in the order that all 3 work in one file. Thanks //Program 1 using System; class MatrixLibrary { public static int[,] Matrix_Multi(int[,] a, int [,]b){ int r1= a.GetLength(0); int c1 = a.GetLength(1); int c2 = b.GetLength(1); int r2 = b.GetLength(0); if(r2 != c2){ Console.WriteLine("Matrices cannot be multiplied together.\n"); return null;    }else { int[,] c = new int[r1, c2]; for (int i = 0; i <...

  • So I am working on an assignment where we make a rudimentary browser history with c#....

    So I am working on an assignment where we make a rudimentary browser history with c#. The requirement is that we need to use a linked list to do this. The issue that I am having is that when I want to go backwards in the history and print it takes from the beginning of the list and not the front. (Example is if I had a list with 1, 2, 3, 4 in it and went back It would...

  • c programming What is the output, to the console, once the following code has been executed?...

    c programming What is the output, to the console, once the following code has been executed? int collatz(int value) { if(value % 2 == 0) return value / 2; else return value * 3 + 1; } int main() { int currentValue = 1: int 1; for (i = 0; i < 5; i++) { printf("%d\n", currentValue); currentValue = collatz(currentValue); } return 0; }

  • 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (...

    10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println () 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ]...

  • Please fix my code so I can get this output: Enter the first 12-digit of an...

    Please fix my code so I can get this output: Enter the first 12-digit of an ISBN number as a string: 978013213080 The ISBN number is 9780132130806 This was my output: import java.util.Scanner; public class Isbn { private static int getChecksum(String s) { // Calculate checksum int sum = 0; for (int i = 0; i < s.length(); i++) if (i % 2 == 0) sum += (s.charAt(i) - '0') * 3; else sum += s.charAt(i) - '0'; return 10...

  • I need a program in c++ the same as below code but I want it to...

    I need a program in c++ the same as below code but I want it to prompt the user to enter the number of elements after that I want it to ask the user to enter the array elements #include<algorithm> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[50]={2,5,4,3}; bool x[100]; int N=4;//number of elements    int k=10;//target sum int sum;//current target sum int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } void backtrace(int n) { if(sum>k) return...

  • Can anyone help me to convert below code from c# to c++? // C#: public class...

    Can anyone help me to convert below code from c# to c++? // C#: public class TernaryTree { private Node m_root = null; private void Add(string s, int pos, ref Node node) { if (node == null) { node = new Node(s[pos], false); } if (s[pos] < node.m_char) { Add(s, pos, ref node.m_left); } else if (s[pos] > node.m_char) { Add(s, pos, ref node.m_right); } else { if (pos + 1 == s.Length) { node.m_wordEnd = true; } else {...

  • Hello, I have a bug in my code, and when I run it should ask the...

    Hello, I have a bug in my code, and when I run it should ask the user to enter the month and then the year and then print out the calendar for the chosen month and year. My problem with my code is that when I run it and I enter the month, it doesn't ask me for the year and it prints all the years of the month I chose. Please help! Code: #include "calendarType.h" #include <iostream> using namespace...

  • I am writing a console application in C#. It is a receipt where the user inputs...

    I am writing a console application in C#. It is a receipt where the user inputs the item then quantity and then price per item. It is then validated through regular expressions. I need to put this in a multidimensional array to print out (command line print), like each row would have a columns for item, quantity, price and then subtotal for each item, but cannot figure it out. I also need the regular expression in a do while loop...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT