Advertisement
hristo_bratanov

Substring count

Jan 27th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class SubstringCount
  5. {
  6.     static void Main()
  7.     {
  8.         Console.Write("search in: ");
  9.         string input = Console.ReadLine();
  10.         string inputCopy = input;
  11.  
  12.         Console.Write("search for: ");
  13.         string searchFor = Console.ReadLine();
  14.         string searchForCopy = searchFor;
  15.  
  16.         int startIndex = 0;
  17.         int count = 0;
  18.  
  19.         input = input.ToLower();
  20.         searchFor = searchFor.ToLower();
  21.         while (true)
  22.         {
  23.             int occur = input.IndexOf(searchFor, startIndex);
  24.             if (occur == -1)
  25.             {
  26.                 break;
  27.             }
  28.  
  29.             else
  30.             {
  31.                 count++;
  32.             }
  33.  
  34.             startIndex = occur + searchFor.Length;
  35.         }
  36.  
  37.         Console.WriteLine("\"{0}\" occures {1} times in \"{2}\"", searchForCopy, count, inputCopy);
  38.         Console.WriteLine(count);
  39.     }
  40.    
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement