Click here to Skip to main content
15,883,952 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one text file.Now my requirement is wants to search word based on user input in any direction( horizontally and vertically).

namespace WordSearch
{
    class Program
    {
       

        public static void Main()
        {

            // Start To Show the file 
            FileStream inFile1 = new FileStream(@"D:\Tricube_Task\Code_Fun\Hello.txt", FileMode.Open, FileAccess.Read);
            using (StreamReader file = new StreamReader(inFile1))
            {
                int counter = 0;
                string ln;

                while ((ln = file.ReadLine()) != null)
                {
                    Console.WriteLine(ln);
                    counter++;
                }
                //file.Close();
                Console.WriteLine($"File has {counter} lines.");
            }

            // End To Show the File


            

            // Start To show the word in the file

            //the path of the file
            FileStream inFile = new FileStream(@"D:\Tricube_Task\Code_Fun\Hello.txt", FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(inFile);

            string record;
            string input;

            Console.Write("Enter The word:");// User Input
            input = Console.ReadLine();
            try
            {
                //the program reads the record and displays it on the screen
                record = reader.ReadLine();

                while (record != null)
                {                  

                    if (record.Contains(input))
                    {
                        Console.WriteLine(record);
                    }
                    record = reader.ReadLine(); 
                }
            }

            finally
            {
                //after the record is done being read, the progam closes
                reader.Close();
                inFile.Close();
            }


            // End Show the word in the file


            // Exit from application
            Console.Write("Press 'Q' to Quit from Program.. ");            
            while (Console.ReadKey().Key != ConsoleKey.Q)
            {

            }
            Console.ReadLine();

            // Exit from application

        }


    }


}


What I have tried:

From this code I can read word horizontally. Need to improve code for any direction.
Posted
Updated 23-Oct-20 8:27am
Comments
Richard MacCutchan 23-Oct-20 7:37am    
You can only find a word in a text file by reading each line and searching horizontally in that line. The concept of a vertical search does not make sense.
ZurdoDev 23-Oct-20 11:46am    
Just a guess, but probably like a word search. If the file had characters in each position on each line then you could search vertically.
Richard MacCutchan 23-Oct-20 12:09pm    
I never thought of that. Despite being a lifelong cruciverbalist.
Sandeep Mewara 23-Oct-20 10:23am    
As Richard shared, explaining what yu mean by Vertical search might give clue to what you are trying to do.
ZurdoDev 23-Oct-20 11:46am    
Just a guess, but probably like a word search. If the file had characters in each position on each line then you could search vertically.

1 solution

Vertical, horizontal, diagonal ... your life with be simpler if you just read all text lines into a collection (list; array).

Strings can be treated as arrays of characters: strings s = "ABC"; var ch = s[1];

(Then there's: StartsWith; EndsWith; Contains; Substring, IndexOf; LastIndexOf; ...)

You've got vertical (list item[]) and horizontal (s[1]).

With diagonal, you vary both at the same time.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900