nadia_dr

Untitled

Aug 17th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. sing System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. class CountingWord
  7. {
  8.     /*Write a program that counts how many times a given word occurs in given text. The first line in the input holds the word.
  9.      * The second line of the input holds the text. The output should be a single integer number – the number of word occurrences.
  10.      * Matching should be case-insensitive. Note that not all matching substrings are words and should be counted. A word is a sequence of letters
  11.      * separated by punctuation or start / end of text.*/
  12.  
  13.     static void Main()
  14.     {
  15.         string word =Console.ReadLine();
  16.         string text = Console.ReadLine();
  17.        
  18.         ExtractText(text, word);
  19.     }
  20.  
  21.     static void ExtractText(string text, string word)
  22.     {
  23.         string[] splitText = text.ToUpper().Split('.','!');
  24.         int counter = 0;
  25.  
  26.         for (int i = 0; i < splitText.Length; i++)
  27.         {
  28.             string[] splitWords = splitText[i].Split(' ','"',',','/','.','@','(',')');
  29.  
  30.             for (int j = 0; j < splitWords.Length; j++)
  31.             {
  32.                 if (splitWords[j].ToUpper() == word.ToUpper())
  33.                 {
  34.                     counter++;
  35.                 }
  36.             }
  37.         }
  38.         Console.WriteLine(counter);
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment