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 :

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 :

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.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
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