Write a C# program to find the number of times a substring appears in the given string


Introduction

I have use Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2. public class stringexercise
  3. {
  4. public static void Main()
  5. {
  6. string str;
  7. string findstring;
  8. int start = 0;
  9. int cnt = -1;
  10. int index = -1;
  11.  
  12. Console.Write("Input the original string : ");
  13. str = Console.ReadLine();
  14. Console.Write("Input the string to be searched for : ");
  15. findstring = Console.ReadLine();
  16.  
  17. while (start != -1)
  18. {
  19. start = str.IndexOf(findstring, index + 1);
  20. cnt += 1;
  21. index = start;
  22. }
  23. Console.Write("The string '{0}' occurs " + cnt + " times.\n", findstring);
  24.  
  25. Console.ReadLine();
  26. }
  27. }

Result

C# language techstudy

     





Related Topics




Popular Posts