Подсчитать сколько раз в текст1 встречается слово - C#
Формулировка задачи:
Помогите сделать все эти задания, сам я не могу.
Вот задания:
1. В тексте1 ввести любую фразу. В текст2 ввести слово. Подсчитать сколько раз в текст1 встречается слово, заданное в текст2.
Решение задачи: «Подсчитать сколько раз в текст1 встречается слово»
textual
Листинг программы
- using System;
- using System.Diagnostics;
- using System.Linq;
- namespace ConsoleAppExperiments
- {
- class TimeMesurer : IDisposable
- {
- private Stopwatch stopwatch = new Stopwatch();
- public TimeMesurer()
- {
- this.stopwatch.Start();
- }
- public void Dispose()
- {
- this.stopwatch.Stop();
- Console.WriteLine("Time elapsed: {0,10:N0}", this.stopwatch.ElapsedMilliseconds);
- Console.WriteLine();
- }
- }
- class UsagaWordCounter
- {
- enum WordCountState
- {
- Init,
- ComparingWord,
- SkippingWord,
- WhiteSpace
- }
- public static int CountWords(string originString, string word)
- {
- int wordCounter = 0;
- CharEnumerator wordCharEnumerator = word.GetEnumerator();
- WordCountState state = WordCountState.Init;
- foreach (Char c in originString)
- {
- if (c == ' ' || Char.IsPunctuation(c))
- {
- switch (state)
- {
- case WordCountState.Init:
- case WordCountState.SkippingWord:
- state = WordCountState.WhiteSpace;
- break;
- case WordCountState.ComparingWord:
- if (!wordCharEnumerator.MoveNext())
- {
- wordCounter++;
- }
- state = WordCountState.WhiteSpace;
- break;
- }
- }
- else
- {
- switch (state)
- {
- case WordCountState.Init:
- case WordCountState.WhiteSpace:
- wordCharEnumerator.Reset();
- wordCharEnumerator.MoveNext();
- state = (c == wordCharEnumerator.Current)
- ? WordCountState.ComparingWord
- : WordCountState.SkippingWord;
- break;
- case WordCountState.ComparingWord:
- if (!wordCharEnumerator.MoveNext() ||
- wordCharEnumerator.Current != c)
- {
- state = WordCountState.SkippingWord;
- }
- break;
- }
- }
- }
- if (state == WordCountState.ComparingWord && !wordCharEnumerator.MoveNext())
- {
- wordCounter++;
- }
- return wordCounter;
- }
- }
- class Program
- {
- private static char[] SEPARATORS = { ' ', ',', '.', '!', ':', ';', '(', ')', '«', '»' };
- static int CountWordWithSplit(string sourseText, string word)
- {
- return sourseText.Split(SEPARATORS, StringSplitOptions.RemoveEmptyEntries).Count(x => x == word);
- }
- static int CountWordManually(string sourceText, string word)
- {
- return UsagaWordCounter.CountWords(sourceText, word);
- }
- static void Main(string[] args)
- {
- string warAndPeace = Resource1.warAndPeace;
- string peaceWord = "мир";
- Console.WriteLine("Source data length: {0:N0}\n", warAndPeace.Length);
- Console.WriteLine("Split, cold iteration");
- using (var mesurer = new TimeMesurer())
- {
- Console.WriteLine("Words found: {0:N0}", CountWordWithSplit(warAndPeace, peaceWord));
- }
- Console.WriteLine("Manual, cold iteration");
- using (var mesurer = new TimeMesurer())
- {
- Console.WriteLine("Words found: {0:N0}", CountWordManually(warAndPeace, peaceWord));
- }
- for (int i = 0; i < 3; i++)
- {
- Console.WriteLine("-----------");
- Console.WriteLine("Split, hot iteration #{0}", i);
- using (var mesurer = new TimeMesurer())
- {
- Console.WriteLine("Words found: {0:N0}", CountWordWithSplit(warAndPeace, peaceWord));
- }
- //GC.Collect();
- //GC.WaitForFullGCComplete();
- Console.WriteLine("Manual, hot iteration #{0}", i);
- using (var mesurer = new TimeMesurer())
- {
- Console.WriteLine("Words found: {0:N0}", CountWordManually(warAndPeace, peaceWord));
- }
- }
- Console.WriteLine("press any key...");
- Console.ReadKey();
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д