Question
I need helping fixing my C# code. Task: Create a prompt to enter a pattern. Create a random string. Have the pattern searched using Boyer
I need helping fixing my C# code. Task: Create a prompt to enter a pattern. Create a random string. Have the pattern searched using Boyer Moore Algorithm. Print results back to Console of what line item and the pattern
1.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace Bruteforce { public class ProgramTest { public static void Main(string[] args) { Program myProgram = new Program("Thank you for running the " + "Boyer Moore Algorithm");
myProgram.InputPattern(); //read in the inputted pattern from user
myProgram.RandomGenerator(); //run the random generator program
//i am trying to run this function so that it can run the string and pattern myProgram.Search();
myProgram.DisplayTime();
Console.Read(); } } }
2.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace Bruteforce { public class Program { string s; string pattern;
public Program(string name) {
}
public void InputPattern() { Console.WriteLine("Enter the pattern you want to search: "); pattern = (Console.ReadLine()).ToUpper(); //read user input
}
public void RandomGenerator() { Random geneSequence = new Random(); // The random number sequence
for (int i = 0; i < 100; i++) { int x = geneSequence.Next(1, 4);
switch (x) { case 1: s = s + 'C'; break;
case 2: s = s + 'T'; break;
case 3: s = s + 'A'; break;
case 4: s = s + 'G'; break; }
}
Console.WriteLine(s); //display results of string }
public void Search() // s is string sequence, pattern is what is inputted from user { List
// boyer moore algorithm is executed here int n; n = s.Length;
int m; m = pattern.Length;
int[] badChar = new int[256];
BadCharHeuristic(pattern, m, ref badChar);
int i = 0;
while (i <= (n - m)) { int j = m - 1;
while (j >= 0 && pattern[j] == s[i + j]) --j;
if (j < 0) { retVal.Add(i); i += (i + m < n) ? m - badChar[s[i + m]] : 1;
}
else { i += Math.Max(1, j - badChar[s[i + j]]); }
}
// i want to be able to print the results including the patterns to the screen.
//this return is an error, how can i get rid of it return retVal.ToArray(); // Console.WriteLine("Pattern found at:{0}", i); // Console.WriteLine(pattern);
}
private static void BadCharHeuristic(string str, int size, ref int[] badChar) { int i;
for (i = 0; i < 256; i++) badChar[i] = -1;
for (i = 0; i < size; i++) badChar[(int)str[i]] = i; }
public void DisplayTime() { var watch = System.Diagnostics.Stopwatch.StartNew(); // the code that you want to measure comes here watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine("Time processed: {0}ms", watch.ElapsedMilliseconds); }
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started