Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text.RegularExpressions;
- using System.IO;
- class CountingAWord
- {
- //Write a program that counts how many times a given word occurs in given text. The first line in the input holds the word.
- //The second line of the input holds the text. The output should be a single integer number – the number of word occurrences.
- //Matching should be case-insensitive. Note that not all matching substrings are words and should be counted.
- //A word is a sequence of letters separated by punctuation or start / end of text.
- static void Main()
- {
- Console.SetIn(new StreamReader(Console.OpenStandardInput(8192))); //increases console input maximum length.
- string searchWord = Console.ReadLine();
- string pattern = @"\b" + searchWord + @"\s*\b";
- string text = Console.ReadLine();
- string[] words = text.Split(new char[] { ' ', ',', '.', ':', '\t', '(', ')', '/' });
- int count = 0;
- foreach (Match match in Regex.Matches(text, pattern, RegexOptions.IgnoreCase))
- {
- count++;
- }
- Console.WriteLine(count);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement