w3resource

C# Sharp Exercises: Count Occurrences of a substring in a string

C# Sharp String: Exercise-61 with Solution

Write a C# Sharp program to count a specific string (case sensitive) in a given text.

Sample Solution-1:

C# Sharp Code:

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "The quick brown fox jumps over the lazy dog.";
            string search_str = "The";
            Console.WriteLine("Original string and search string:\n" +text +" \n" +search_str);
            Console.WriteLine("Occurrences of the said substring in the string: " + test(text, search_str));
            text = "Red Green Black RedRed";
            search_str = "Red";
            Console.WriteLine("\nOriginal string and search string:\n" + text + " \n" + search_str);
            Console.WriteLine("Occurrences of the said substring in the string: " + test(text, search_str));
            text = "Red Green Black";
            search_str = "Pink";
            Console.WriteLine("\nOriginal string and search string:\n" + text + " \n" + search_str);
            Console.WriteLine("Occurrences of the said substring in the string: " + test(text, search_str));

        }  
        public static int test(string text, string search_str)
        {
            int count = 0;
            for (int i = 0; i < text.Length - search_str.Length + 1; i++)
            {
                if (text.Substring(i, search_str.Length) == search_str)
                {
                    count++;
                }
            } 
            return count;
        }
    }
} 

Sample Output:

Original string and search string:
The quick brown fox jumps over the lazy dog. 
The
Occurrences of the said substring in the string: 1

Original string and search string:
Red Green Black RedRed 
Red
Occurrences of the said substring in the string: 3

Original string and search string:
Red Green Black 
Pink
Occurrences of the said substring in the string: 0

Flowchart :

Flowchart: C# Sharp Exercises - Count Occurrences of a substring in a string.

Sample Solution-2:

C# Sharp Code:

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "The quick brown fox jumps over the lazy dog.";
            string search_str = "The";
            Console.WriteLine("Original string and search string:\n" +text +" \n" +search_str);
            Console.WriteLine("Occurrences of the said substring in the string: " + test(text, search_str));
            text = "Red Green Black RedRed";
            search_str = "Red";
            Console.WriteLine("\nOriginal string and search string:\n" + text + " \n" + search_str);
            Console.WriteLine("Occurrences of the said substring in the string: " + test(text, search_str));
            text = "Red Green Black";
            search_str = "Pink";
            Console.WriteLine("\nOriginal string and search string:\n" + text + " \n" + search_str);
            Console.WriteLine("Occurrences of the said substring in the string: " + test(text, search_str));

        }  
        public static int test(string text, string search_str)
        {
            return (text.Length - text.Replace(search_str, "").Length) / search_str.Length;

        }
    }
}

Sample Output:

Original string and search string:
The quick brown fox jumps over the lazy dog. 
The
Occurrences of the said substring in the string: 1

Original string and search string:
Red Green Black RedRed 
Red
Occurrences of the said substring in the string: 3

Original string and search string:
Red Green Black 
Pink
Occurrences of the said substring in the string: 0

Flowchart :

Flowchart: C# Sharp Exercises - Count Occurrences of a substring in a string.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous C# Sharp Exercise: Count the number of duplicate characters in a string.
Next C# Sharp Exercise: Alphabet position in a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter