Search code examples
c#linear-search

Linear Search Using Text File C#


So I have a text file which I would like to search through for a number and if it is found then outputs a message number is found and if not then not found.

Currently I have read the text file into a string and asked the user what number they would like to search for. The part I am stuck on is the code for the linear search for a string. I want to look for a string because other files I will use may contain words.

Code So far:

    public static void search()   // Search for values
    {
        Console.WriteLine("Enter 1 to search through days");
        int operation = Convert.ToInt32(Console.ReadLine());

        if (operation == 1)
        {

            String[] myArray = File.ReadAllLines("Data1/Day_1.txt");

            Console.WriteLine("Enter number to search");
            String myString = Console.ReadLine();
        }
    }

I have seen some code for a linear search but not sure how to apply it properly to my example. I understand how it works but problem is with getting code in the correct order. Not too good at coding in c#. Any help would be much appreciated. Thanks

Generic Linear Search Code :

static int Search(string[] list, string elementSought)
{
bool found = false;
int max = list.Length - 1;
int currentElement = 0;

do
{
    if (list[currentElement] == elementSought)
    {
        found = true;
    }
    else
    {
        currentElement = currentElement + 1;
    }
} while (!(found == true || currentElement > max));

if (found == true)
{
    return currentElement;
}
else
{
    return -1;
}
}

Update:

   public static void search()   // Search for values
    {
  Console.WriteLine("1=Day 2=Depth");
  int operation = Convert.ToInt32(Console.ReadLine());
  if (operation == 1)
        {
            String[] myArray = File.ReadAllLines("Data1/Day_1.txt");
        }

        else if (operation == 2)
        {
            String[] myArray = File.ReadAllLines("Data1/Months_1.txt");
        }


            Console.WriteLine("Enter number to search");
            String myString = Console.ReadLine();
            int i = 0;
            int j = 0;
            var regex = new Regex(myString);
            foreach (string array in myArray)
            {
                if (regex.IsMatch(array))
                {
                    i++;
                }

                else if (regex.IsMatch(array))
                {
                    j++; 
                }
            }

            if (i > 0)
            {
                Console.WriteLine("Found match! - {1} Appeared {0} time(s)",i,myString);
            }

            else if(j == 0)
            {
                Console.WriteLine("No Match for {0} in Data", myString);
            }

            Console.ReadLine();
        }

How do I change it so that if I choose the second file that it selects that as the one to use as my Array?


Solution

  • You need to loop through your array of strings in search of a match. Use a regex to check for a match.

    Console.WriteLine("Enter number to search");
    String myString = Console.ReadLine();
    
    var regex = new Regex(myString);
    foreach (string array in myArray)
        if (regex.IsMatch(array))
            Console.WriteLine("Found match!");