2

I have a text file that a list of these words on it

Laptop
Laser
Macho
Sam
Samantha
Mulder
Microphone
Aardvark

And what I want to do is have user input type in a word and the console will basically respond with we have your word or we do not have your word. This is my code so far:

TextReader file = new StreamReader("Files/Exercise_Files/Words.txt");

Console.WriteLine("Welcome to FindWord");
string wordInput;
Console.WriteLine("Type in a word so we can try and find it: ");
wordInput = Console.ReadLine();

string sLine = file.ReadToEnd();
if (String.Compare(wordInput, sLine, true) == 0)
{
    Console.WriteLine("We have found your word!");               
}
else 
{
    Console.WriteLine("We have not found your word");
}

file.Dispose();

I have tried doing a couple of versions trying to solve this and one included adding a for-each loop but that confused me quite a bit. I'm not 100% sure when to use a for-each loop. I also want to have string comparison be case insensitive but the code I have now will always say word not found no matter what I input.

stuartd
  • 70,509
  • 14
  • 132
  • 163
mosquird
  • 39
  • 1
  • 8
  • 1
    The first thing you need to learn is [how to debug](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Pay particular attention to what `wordInput` and `sLine` contain. Then look up what [`String.Compare`](https://learn.microsoft.com/en-us/dotnet/api/system.string.compare?view=netframework-4.7.1#System_String_Compare_System_String_System_String_System_Boolean_) does (hint: it doesn't do what you think it does). – Dour High Arch Dec 04 '18 at 17:58
  • @DourHighArch okay, sorry, I think you can tell I am quite new to coding. So I would have to compare wordInput to a different string, right? Because sLine just reads the contents – mosquird Dec 04 '18 at 18:14
  • You've been given several proposals; test each one in the debugger and see which is best for you. Please do not just copy one of the proposals and declare yourself finished, you should be able to understand why your initial attempt did not work, and what every proposal does. – Dour High Arch Dec 04 '18 at 18:55

3 Answers3

1

Load file into array

string[] words = File.ReadAllLines(path);

Then use LINQ to find

if (words.Any(w => string.Equals(w, input, StringComparison.InvariantCultureIgnoreCase)))
    // do your thing here

Performance Contains vs Any

Contains is quicker but in this case difference will not be visible since searched collection is small.

public class Program
{

    public static HashSet<string> _hs = new HashSet<String>(Enumerable.Range(1,1000).Select(x=> "Item " + x));
    public static string[] _arr = Enumerable.Range(1,1000).Select(x=> "Item " + x).ToArray();

    public static void Main()
    {

        var sw = new Stopwatch();
        sw.Start();
        bool f;
        for (int i = 1; i < 1001; i++)
        {
            //f = _hs.Contains("Item " + i, StringComparer.OrdinalIgnoreCase);
            f = _arr.Contains("Item " + i, StringComparer.OrdinalIgnoreCase);

        }
        Console.WriteLine(sw.Elapsed);

        sw.Restart();
        for (int i = 1; i < 1001; i++)
        {
            f = _hs.Any(w => string.Equals(w, "Item " + i, StringComparison.InvariantCultureIgnoreCase));
        }
        Console.WriteLine(sw.Elapsed);

    }
}
T.S.
  • 18,195
  • 11
  • 58
  • 78
  • 1
    Shouldn't `string words` be `string[] words`? – Jaskier Dec 04 '18 at 17:57
  • 1
    ReadAllLines() gives a string[], not a string. Other than that, this was comparable to what I was going to suggest. – Dave Smash Dec 04 '18 at 17:58
  • 1
    @Symon Thanks - just a typo – T.S. Dec 04 '18 at 17:58
  • 1
    @ElementalPete Thanks - just a typo. Sorry to spoil your answer – T.S. Dec 04 '18 at 17:59
  • Out of curiosity, would this be an efficient solution? I was thinking of loading each word into a `HashSet` and doing a `.Contains`, but your solution is more elegant. –  Dec 04 '18 at 18:01
  • @Matt using `Any` is pretty efficient because it finds first instance and returns. It also doesn't modify or create any new collection or elements – T.S. Dec 04 '18 at 18:04
  • 1
    @Matt you were looking for this https://stackoverflow.com/a/4445255/1704458 In current case no difference will be made – T.S. Dec 04 '18 at 18:11
  • @Matt Contains is quicker (in this case) - I posted test in my answer. You can fiddle it – T.S. Dec 04 '18 at 18:32
1

You can use the File.ReadAllLines() method to read the file lines into an array, and then the Linq extension method Contains to determine if a word exists in that list (and you can do a case-insensitive search):

static void Main(string[] args)
{
    var filePath = @"c:\temp\temp.txt";
    var words = File.ReadAllLines(filePath);

    Console.Write("Enter a search term: ");
    var searchTerm = Console.ReadLine();

    if (words.Contains(searchTerm, StringComparer.OrdinalIgnoreCase))
    {
        Console.WriteLine("We have your word!");
    }
    else
    {
        Console.WriteLine("We do not have your word");
    }

    Console.ReadKey();
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

String.Compare , compares two strings. I think, you may want to use Contains method. Please see below-

        if (sLine.Contains(wordInput))
        {
            Console.WriteLine("We have found your word!");
        }
        else
        {
            Console.WriteLine("We have not found your word");
        }
Gaurav
  • 623
  • 5
  • 11