Note that there are some explanatory texts on larger screens.

plurals
  1. POstring linear search c#
    primarykey
    data
    text
    <p>I am currently doing a small C# exercise that deals with searching related terms/words in an text file and the program will write out all sentences in the text file that include the searched word. For instance, I enter the word: "example", and what the program will do is go through all the sentences in a text file and pull out those sentences that have the word "example" in them. </p> <pre><code>The text file is structured as so: &lt;sentenceDesignator&gt; &lt;text&gt; sentence 1: bla bla bla bla example of a sentence //each line contains a sentence sentence 2: this is not a good example of grammar sentence 3: bla is not a real word, use better terms </code></pre> <p>What I would like to be able to do is use a linear search to go through all the lines in the text file and write out all sentences that contain the searched string term.</p> <p>My code so far:</p> <pre><code> String filename = @"sentences.txt"; if (!File.Exists(filename)) { // Since we just created the file, this shouldn't happen. Console.WriteLine("{0} not found", filename); return; } else { Console.WriteLine("Successfully found {0}.", filename); } //making a listof type "Sentence" to hold all the sentences List&lt;Sentence&gt; sentences = new List&lt;Sentence&gt;(); //the next lines of code... StreamReader reader = File.OpenText(filename); //first, write out all of the sentences in the text file //read a line(sentence) from a line in the text file string line = reader.ReadLine(); while (line != null) { Sentence s = new Sentence(); //we need something to split data... string[] lineArray = line.Split(':'); s.sentenceDesignator = lineArray[0]; s.Text = lineArray[1]; Console.Write("\n{0}", line); line = reader.ReadLine(); } //so far, we can write out all of the sentences in the text file. Console.Write("\n\nOK!, search a term to diplay all their occurences: "); string searchTerm = Console.ReadLine(); if(!line.Contains(searchterm)) { Console.Write("\nThat term does not exist in any sentence."); } else { foreach (Sentence ss in sentences) { if (ss.sentenceDesignator.Contains(queryName)) { //I need help here } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload