Advertisement
Razhagal

Counting a Word in Text

Mar 31st, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. class CountingWordsInText
  8. {
  9.     static int CompareWords(string inputWord, string inputText)
  10.     {
  11.         string[] allWords = inputText.Split(
  12.             new char[] { ' ', '.', ',', '"', '@', '!', '?', '/', '\\', ':', ';', '(', ')' },
  13.             StringSplitOptions.None);
  14.  
  15.         int counter = 0;
  16.  
  17.         for (int i = 0; i < allWords.Length; i++)
  18.         {
  19.             //string.Equals give better options to compare strings (like ignore casing)
  20.             if (string.Equals(allWords[i], inputWord, StringComparison.OrdinalIgnoreCase))
  21.             {
  22.                 counter++;
  23.             }
  24.         }
  25.         return counter;
  26.     }
  27.     static void Main()
  28.     {
  29.         string keyWord = Console.ReadLine();
  30.         string someText = Console.ReadLine();
  31.  
  32.         int result = CompareWords(keyWord, someText);
  33.         Console.WriteLine(result);
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement