Advertisement
Teodor92

SubstringFinder.cs

Jan 24th, 2013
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | None | 0 0
  1. /* Write a program that finds how many times a substring
  2.  * is contained in a given text (perform case insensitive search).
  3.  */
  4.  
  5. using System;
  6.  
  7. public class SubstringFinder
  8. {
  9.     public static void Main()
  10.     {
  11.         ////string test = "inmyinajin";
  12.         ////Console.WriteLine(test.IndexOf("in"));
  13.         ////Console.WriteLine(test.IndexOf("in",1,test.Length - 1));
  14.         string input = Console.ReadLine();
  15.         string searchSub = Console.ReadLine();
  16.         int subStringCounter = 0;
  17.         int startIndex = 0;
  18.         bool noString = false;
  19.         while (!noString)
  20.         {
  21.             if (input.IndexOf(searchSub, startIndex) != -1)
  22.             {
  23.                 subStringCounter++;
  24.                 startIndex = input.IndexOf(searchSub, startIndex) + 1;
  25.             }
  26.             else
  27.             {
  28.                 noString = true;
  29.             }
  30.         }
  31.  
  32.         Console.WriteLine(subStringCounter);
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement