Advertisement
vladeto87

CountingAWordInAText

Apr 10th, 2014
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 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.  
  8. class CountingAWordInAtext
  9. {
  10.     static void Main()
  11.     {
  12.         Console.Write("Enter the word which will be counted in the text : ");
  13.         string searchWord = Console.ReadLine();
  14.  
  15.         Console.WriteLine("Enter the text.");
  16.         string text = Console.ReadLine();
  17.  
  18.         string lowerWord = searchWord.ToLower();
  19.  
  20.         string[] textArray = text.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',', '"', '(', ')', '/', '@' },
  21.             StringSplitOptions.RemoveEmptyEntries);
  22.  
  23.         for (int i = 0; i < textArray.Length; i++)
  24.         {
  25.             textArray[i] = textArray[i].ToLower();
  26.         }
  27.  
  28.  
  29.         int counter = 0;
  30.         for (int i = 0; i < textArray.Length; i++)
  31.         {
  32.  
  33.             if (textArray[i].Contains(lowerWord) && (textArray[i].Length == lowerWord.Length))
  34.             {
  35.                 counter++;
  36.             }
  37.             else
  38.             {
  39.                 continue;
  40.             }
  41.         }
  42.         Console.WriteLine(counter);
  43.  
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement