0

I am trying to figure out how to make an interactive word search console application. The application searches for the characters the user has inputted in a text file and outputs all the words that contain the characters in the input word. The way it should work is that the user inputs a search entry and the console shows the results AS the user types the characters, updating after each new character.

I now have it working in a way that requires the user to press enter to search. What do I need to change?

public class Search
{

    public static void Main()
    {
        const string PATH = "kotus_sanat.txt";
        string[] words = File.ReadAllLines(PATH);
        Console.Write("search: ");
        string entry = Console.ReadLine();

        List<string> result = new List<string>();
        result= FindWords(entry, words);
        for (int i = 0; i < result.Count; i++)
        {
            Console.WriteLine(result[i]);
        }
    }

    public static List<string> FindWords(string entry, string[] words)
    {
        List<string> results = new List<string>();
        for (int i = 0; i < words.Length; i++)
        {
            if (ContainsAllChars(words[i], entry) == true) results.Add(words[i]);
        }
        return results;
    }

    public static bool ContainsAllChars(string word, string chars)
    {
        StringBuilder charsNow = new StringBuilder(chars);
        StringBuilder wordNow = new StringBuilder(word);
        int i = 0;
        while (i < chars.Length)
        {
            char charNow = chars[i];
            int charIndex = wordNow.IndexOf(charNow);
            if (charIndex >= 0)
            {
                wordNow[charIndex] = (char)0;
            }
            else return false;
            i++;
        }
        return true;
    }
}

public static class Helper
{

    public static int IndexOf(this StringBuilder sb, char c, int startIndex = 0)
    {
        for (int i = startIndex; i < sb.Length; i++)
            if (sb[i] == c)
                return i;
        return -1;
    }
}

1 Answers1

0

You may create your solution around the answer to this question. Just store what you want to show, react to key-presses directly and update the screen.

Community
  • 1
  • 1
Jonas Köritz
  • 2,606
  • 21
  • 33