stefan1919

CountOccurrences

Sep 27th, 2015
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 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. namespace ConsoleApplication7
  8. {
  9.     class Program
  10.     {
  11.  
  12.         static void Main(string[] args)
  13.         {
  14.             string input = Console.ReadLine();
  15.             string pattern = Console.ReadLine();
  16.             Console.WriteLine(Occurrences(input, pattern));
  17.         }
  18.        static int Occurrences(string longWord, string shortWord)
  19.         {
  20.             int counter = 0;
  21.             int lengthPattern = shortWord.Length;
  22.             for (int i = 0; i <= longWord.Length - lengthPattern; i++)
  23.             {
  24.                 if (longWord.ToLower().IndexOf(shortWord.ToLower(), i, lengthPattern) != -1)
  25.                 {
  26.                     counter++;
  27.                 }
  28.             }
  29.             return counter;
  30.         }
  31.        
  32.    
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment