The language of code in C#.
Finds the number of times a word occurs by itself (as a complete word) or as part of another word in the given "wordFragment" substring. For example in the string "The bon in bonbon", we count 2 occurrences of bon. Please write a simple code without the use of advanced features such as LINQ. It's recommended using the Dictionary feature. Returns the number of times wordFragment occurs in the file that WordCounter is associated with.
(((public class WordCounter
{
private readonly Dictionary<string, int> map = new
Dictionary<string, int>();)))))/// given for reference.
public int CountOccurrences(string wordFragment)
{
implementation required
}
using System;
public class counter
{
public static int CountOccurrences(string str,string
wordFragment)
{
// split the string by spaces
string[] a = str.Split(' ');
// search for pattern in string
int count = 0;
for (int i = 0; i < a.Length; i++)
{
// if match found increase count
if (wordFragment.Equals(a[i]))
count++;
}
return count;
}
// Driver code
public static void Main()
{
string str = "The bon in bonbon ";
string word = "bon";
Console.Write(CountOccurrences(str,
word));
} }
Get Answers For Free
Most questions answered within 1 hours.