Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

using System;
public class stringexercise
{
    public static void Main()
    {
        string str;
        string findstring;
        int start = 0;
        int cnt = -1;
        int index = -1;
 
        Console.Write("Input the original string : ");
        str = Console.ReadLine();
        Console.Write("Input the string to be searched for : ");
        findstring = Console.ReadLine();
 
        while (start != -1)
        {
            start = str.IndexOf(findstring, index + 1);
            cnt += 1;
            index = start;
        }
        Console.Write("The string '{0}' occurs " + cnt + " times.\n", findstring);
 
        Console.ReadLine();
    }
}

Result:

Input the original string : techstudy - the compelte debugging solution

Input the string to be searched for : techstudy

The string 'techstudy' occurs 1 times.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a C# program to check the username and passw... >>
<< Write a C# program to compare (less than, greater ...