Linear Search in C# (searching through a text file)

The linear search is a very simple searching algorithm. The way it works is that it starts from the beginning of an Array or List or collection and goes through checking whether the given value or object exists in a data structure. This type of searching algorithm is also called a ‘brute force’ search.

For example, a linear search is very similar to looking in a phonebook, and you start with section ‘A’ and look and look until you find the exact number you’re looking for, hence the name ‘brute-force search’. The Linear Search has O(n) time complexity, so its recommended to use with smaller collections rather than large collections. With larger collections a binary search is more efficient, which will be the topic of my next post.

The following code is a linear search made to search a text file for a certain term the user enters. The objective from this post is first learning how the linear search works, and second it presents an opportunity to see how one can open a text file, read in information, and write to a text file (i.e. learning input/output) using C#.

using System;
using System.IO; //this is required for reading and writing to disk, so we can use StreamReader and StreamWriter to work with text files

class Program
        static void Main()

            DisplayTextFile();//this method is called so that when the application loads, the lines of text get displayed automatically.
            //the method that is called above is important because if the file has been opened in another part of the program, we can open it in this part and search and write tweets.
            //if I load once in Main, I cannot load the file again in this part of the program. There is a way around it, but for now, calling this method is simple.

            // File.OpenText allows us to read the contents of a file by establishing
            // a connection to a file stream associated with the file.
            StreamReader reader = File.OpenText("textfile.txt");//reading from "textfile.txt" in bin/debug

            // We can now read data from the file using ReadLine.
            String searchingLine = reader.ReadLine();

                Console.Write("\n\nEnter a search term to display all lines containing the term: ");
//prompt the user to enter a search term to look for in the text file
                string searchTerm = Console.ReadLine();

                while (searchingLine != null)
                    // We can use String.Split to separate a line of data into fields.

                    String[] lineArray = searchingLine.Split(' ');
//we assign a string array for each line read in the text file so that when we read in a line, its stored in an array

                    for (int x = 0; x < lineArray.Length; x++)//because name is already stored at the first index (0), that can only mean that the entire tweet is stored
//therefore, a for loop has to be used to go through the entire line, and check, if any index in lineArray //== the search term
                        if (lineArray[x] == searchTerm)
                            Console.WriteLine();//adding a writeline here forces every line of text to be printed on a new line
                            for (int i = 0; i < searchingLine.Length; i++)
                            {//go though the searchingLine and write out the entire tweet.
                                Console.Write("{0}", searchingLine[i].ToString());

                    searchingLine = reader.ReadLine();

            Console.ReadLine();//keep the window open

static void DisplayTextFile()//this method will do one thing only: display all the text in the file
            //this is only a simple function, it does not return anything, so it is void
            StreamReader reader = File.OpenText("textfile.txt");
            String line = reader.ReadLine();
            using (StreamReader alternateReader = File.OpenText("textfile.txt"))
                line = alternateReader.ReadLine();

                while (line != null)
                    Console.WriteLine("{0}", line);

                    line = alternateReader.ReadLine();

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s