Advertisement
Karlie

C#Homework7_CountingWordInText

May 6th, 2014
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.IO;
  4.  
  5. class CountingAWord
  6. {
  7. //Write a program that counts how many times a given word occurs in given text. The first line in the input holds the word.
  8. //The second line of the input holds the text. The output should be a single integer number – the number of word occurrences.
  9. //Matching should be case-insensitive. Note that not all matching substrings are words and should be counted.
  10. //A word is a sequence of letters separated by punctuation or start / end of text.
  11.  
  12. static void Main()
  13. {
  14. Console.SetIn(new StreamReader(Console.OpenStandardInput(8192))); //increases console input maximum length.
  15.  
  16. string searchWord = Console.ReadLine();
  17. string pattern = @"\b" + searchWord + @"\s*\b";
  18. string text = Console.ReadLine();
  19. string[] words = text.Split(new char[] { ' ', ',', '.', ':', '\t', '(', ')', '/' });
  20. int count = 0;
  21. foreach (Match match in Regex.Matches(text, pattern, RegexOptions.IgnoreCase))
  22. {
  23. count++;
  24. }
  25. Console.WriteLine(count);
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement