Hello, I want to make a program that reads text from a file and displays only those sentences that are made up of a specified number of words.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace Stroki { class Program { static void Main() { string[] NewFile = File.ReadAllLines(@"полный адрес файла"); foreach (string str in NewFile)//считывает весь файл? { Console.WriteLine(str);//вывело строки файла } Console.Write("Введите количество слов = "); string buf = Console.ReadLine(); int n = int.Parse(buf); //тут фором пробегаемся посимвольно по строкам и находим разделительные знаки или пробелы.count++ //условие(если count в строке равен указаному вначале n,то выводим необходимое предложение или несколько,если они одинаковы по количеству слов в предложении) Console.ReadKey(); } } } 

Sample file:

 Корова,мясо,трава,луга. Экзамены,зачёты,курсовые. Лето,зима. 

PS What if all the sentences are in one line?

  • one
    "string"! = "sentence" - Igor
  • one
    Well, this "if." If this is the case, add it to the question. - VladD
  • one
  • one
    @ixSci: How will the dictionary help? Well, "in most cases correct" means a partial solution. Imagine a function that determines the length of an array in most cases correctly - you won’t use it? - VladD
  • one
    @VladD, it depends on the task. The correct Russian text is not so difficult to analyze; in the example you gave above, the wrong spelling of the word “shit” is used. It all depends on the task, for many tasks “understanding” of the text is not required. - ixSci

2 answers 2

In order not to load memory, it is better to use the StreamReader class and read it line by line. Next, first using the Split method, split the resulting string into dots, exclamation marks, and so on.

 line.Split(".!?".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) 

Array of sentences by spaces and punctuation

 int count = sentence.Split(" .,:;".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Length 

Well, then I think it will be clear

    As already noted in the comments above, you need to use the Split method of the String class to split the text into parts that are sentences. To do this, in the Split method, you can transfer punctuation marks that end a sentence. Next, for each individual sentence, calculate the length