w3resource

C#: Number of times a substring appeared in a string

C# Sharp String: Exercise-63 with Solution

Write a C# Sharp program that takes a string that repeats a substring. Count the number of times the substring appears.

Sample Data:
("aaaaaa") -> 6
("abababab") -> 4
("abcdabcdabcd") -> 3

Sample Solution-1:

C# Sharp Code:

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "aaaaaa";
            Console.WriteLine("Original strings: " +text);
            Console.WriteLine("Number of times a substring appeared in the said string: " + test(text));
            text = "abababab";
            Console.WriteLine("Original strings: " + text);
            Console.WriteLine("Number of times a substring appeared in the said string: " + test(text));
            text = "abcdabcdabcd";
            Console.WriteLine("Original strings: " + text);
            Console.WriteLine("Number of times a substring appeared in the said string: " + test(text));
        }
        public static int test(string text)
        {
            return text.Split(text[0]).Count() - 1;
        }
    }
} 

Sample Output:

Original strings: aaaaaa
Number of times a substring appeared in the said string: 6
Original strings: abababab
Number of times a substring appeared in the said string: 4
Original strings: abcdabcdabcd
Number of times a substring appeared in the said string: 3

Flowchart :

Flowchart: C# Sharp Exercises - Number of times a substring appeared 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 = "aaaaaa";
            Console.WriteLine("Original strings: " +text);
            Console.WriteLine("Number of times a substring appeared in the said string: " + test(text));
            text = "abababab";
            Console.WriteLine("Original strings: " + text);
            Console.WriteLine("Number of times a substring appeared in the said string: " + test(text));
            text = "abcdabcdabcd";
            Console.WriteLine("Original strings: " + text);
            Console.WriteLine("Number of times a substring appeared in the said string: " + test(text));
        }
        public static int test(string text)
        {
            int ctr = text.IndexOf(text[0], 1);
            return ctr < 0 ? 1 : text.Length / ctr;
        }
    }
}

Sample Output:

Original strings: aaaaaa
Number of times a substring appeared in the said string: 6
Original strings: abababab
Number of times a substring appeared in the said string: 4
Original strings: abcdabcdabcd
Number of times a substring appeared in the said string: 3

Flowchart :

Flowchart: C# Sharp Exercises - Number of times a substring appeared in a string.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous C# Sharp Exercise: Alphabet position in a string.
Next C# Sharp Exercise: Longest abecedarian word in a array of words.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.




We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook