ellapt

T14.4.SubstrCount

Feb 3rd, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. using System;
  2.  
  3. class SubstrCount
  4. {
  5. static void Main()
  6. {
  7. Console.WriteLine("Find the count a substring appears in a given text (case insensitive search)\n");
  8.  
  9. /* Sample text: We are living in an yellow submarine. We don't have anything else.
  10. Inside the submarine is very tight. So we are drinking all the day. We will move out of it in 5 days. */
  11.  
  12. Console.Write("Enter the text: ");
  13. string text = (Console.ReadLine()).ToLower();
  14. Console.Write("Enter the substring: ");
  15. string sbAnyCase = Console.ReadLine();
  16. string sb = sbAnyCase.ToLower();
  17.  
  18. int index = text.IndexOf(sb);
  19. int cnt=0;
  20. while (index != -1)
  21. {
  22. cnt++;
  23. index=text.IndexOf(sb,index+1);
  24. }
  25. Console.WriteLine("The substring {0} appears in the text {1} times.", sbAnyCase,cnt);
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment