1

I need help in displaying the word from a text file in a Console Application. For example, my input string would be "the" and the code will read through the text file and output the words containing "the" such as "The" and "father". I have the code ready but its outputting the entire sentence including the word rather than the word itself. The code looks like this:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace QuizTakeHome
{
    class Program
    {
        static void Main(string[] args)
        {
            string line;
            int counter = 0;

            Console.WriteLine("Enter a word to search for: ");
            string userText = Console.ReadLine();

            string file = "Gettysburg.txt";
            StreamReader myFile = new StreamReader(file);

            int found = 0;

            while ((line = myFile.ReadLine()) != null)
            {
                counter++;
                int index = line.IndexOf(userText, StringComparison.CurrentCultureIgnoreCase);
                if (index != -1)
                {
                    //Since we want the word that this entry is, we need to find the space in front of this word
                    string sWordFound = string.Empty;
                    string subLine = line.Substring(0, index);
                    int iWordStart = subLine.LastIndexOf(' ');
                    if (iWordStart == -1)
                    {
                        //If there is no space in front of this word, then this entry begins at the start of the line
                        iWordStart = 0;
                    }

                    //We also need to find the space after this word
                    subLine = line.Substring(index);
                    int iTempIndex = subLine.LastIndexOf(' ');
                    int iWordLength = -1;
                    if (iTempIndex == -1)
                    {    //If there is no space after this word, then this entry goes to the end of the line.
                        sWordFound = line.Substring(iWordStart);
                    }
                    else
                    {
                        iWordLength = iTempIndex + index - iWordStart;
                        sWordFound = line.Substring(iWordStart, iWordLength);

                    }

                    Console.WriteLine("Found {1} on the sentence: {1} on line number: {0}", counter, sWordFound, line);
                    found++;
                }
            }
            Console.WriteLine("A total of {0} occurences found", found);
        }
    }
   }

And the output looks like:

Output Snapshot

Can anybody help?

sharvil111
  • 4,301
  • 1
  • 14
  • 29
need help
  • 37
  • 5
  • 1
    Well, you read the file line by line, and search the enire line for a match. You should either read the file word by word, or split the lines and THEN do the IndexOf – Luc Morin Dec 01 '15 at 05:15
  • do you think you can show me how that's done? – need help Dec 01 '15 at 05:17
  • 1
    well, first you need to determine what constitute a "word". Words can be separated by spaces, but also by other characters, comas, semi-colons etc. what kind of input are we looking at here ? – Luc Morin Dec 01 '15 at 05:24
  • My input string is "the". I have everything ready but my sWordFound is outputting "the" but with the rest of the sentence afterwards. I just need to cut off the part that comes after "the". – need help Dec 01 '15 at 05:26
  • Take a look at this for example: http://stackoverflow.com/questions/10239518/how-do-i-split-a-phrase-into-words-using-regex-in-c-sharp – Luc Morin Dec 01 '15 at 05:26
  • The thing is, I don't need to split it by word by word. I think the problem with my code is the word length where it outputs past my input string of "the". I need the entire line because that's part of the output, but my sWordFound is outputting "the + ______(rest of sentence)". I need to make this just "the" – need help Dec 01 '15 at 05:29

3 Answers3

2

You can create tokens from your sentence and check every token :

found = 0;
String[] tokens = line.Split(new char[] {' '});
foreach (String token in tokens) {
    if (token.IndexOf(userText, StringComparison.OrdinalIgnoreCase) != -1) {
        Console.WriteLine(token); // Do your stuff here
        found++; //increment to know how many times you found the word in the current line
    }
}
counter += found; //counter will contains all occurences in lines

This snippet use your code (variables). To create our tokens we have to split the current line, to do it we used String.Split

I think this is the best way to do it without regex functions. I hope it can help you.

Ankirama
  • 498
  • 4
  • 14
1

Your Console.WriteLine is wrong.

Use this:

Console.WriteLine("Found {1} on the sentence: {2} on line number: {0}", counter, userText, sWordFound);

The Muffin Man
  • 19,585
  • 30
  • 119
  • 191
0

Here is a code to do that. Remember there are many other ways too.

    var myfilter = "the";
    var lines = File.ReadAllLines(@"C:\myfile.txt");
    var wordsCsv = lines.Select(l => l.Split(new[] { ' ' }).Where(w => w.ToLower().Contains(myfilter.ToLower()))).Where(w => w.Any()).Aggregate("", (current, wordsList) => current + "," + string.Join(",", wordsList));
Kosala W
  • 2,133
  • 1
  • 15
  • 20