Advertisement
Spaskich

Untitled

Sep 26th, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class CountStringOccurrences
  5. {
  6.     static void Main()
  7.     {
  8.         string input = Console.ReadLine();
  9.         string searchFor = Console.ReadLine();
  10.  
  11.         Console.WriteLine(CountSubstrings(input, searchFor));
  12.     }
  13.  
  14.     static int CountSubstrings (string text, string substring)
  15.     {
  16.         int counter = 0, index = 0;
  17.  
  18.         while ((index < text.Length) && index != -1)
  19.         {
  20.             counter++;
  21.             index = text.IndexOf(substring, index + substring.Length);
  22.         }
  23.  
  24.         return counter;
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement