Can anyone help. I have to write two programs, one in C# and one in Java Script,
ID: 3918595 • Letter: C
Question
Can anyone help. I have to write two programs, one in C# and one in Java Script, to complete the same outcome.
My main/first issue is that I am not too sure how to write a function using Visual Studio Code that takes a file as an input from the command line prompt.
Given a text file and an integer k, you are to print the words (and their frequencies of occurrence) whose frequencies of occurrence are the k largest (upper limit) in order of decreasing frequency; breaking any ties in increasing words’ alphabetical order. Words are consecutive sequences of ASCII alphabetical letters (A-Za-z), where letter case is ignored, and all other characters are ignored.
Explanation / Answer
/*
As per my understanding Solved the problem
Let me know in case any doubt
*/
using System;
using System.IO;
public class CountStringsLineByLine
{
static void Main()
{
//Read file
string[] words = File.ReadAllLines("wordsFile.txt"); // create file which contain list of words
int[] occurrences = new int[words.Length];
using (StreamReader text = File.OpenText("textLineByLine.txt"))//Create file contain text. we are going to read this file line by line and find words
{
string line;
while ((line = text.ReadLine()) != null)
{
for (int i = 0; i < words.Length; i++)
{
string word = words[i];
int wordOccurrences =
CountOccurrencesIgnoreCase(word, line);
occurrences[i] += wordOccurrences;
}
}
}
// Print the result
using (StreamWriter result = File.CreateText("StoreResult.txt"))//We wre going to write result in this file
{
for (int i = 0; i < words.Length; i++)
{
result.WriteLine("{0} count is-> {1}",
words[i], occurrences[i]);
}
}
}
static int CountOccurrencesIgnoreCase(
string substring, string text)
{
int count = 0;
int index = -1;
while (true)
{
index = text.IndexOf(substring, index + 1,
StringComparison.OrdinalIgnoreCase);
if (index == -1)
{
// No match found
break;
}
count++;
}
return count;
}
}
/*
Other way to get output
*/
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Linq;
class WordCounter {
static void Main() {
string inputFileName = "text.txt"; //File Path with file name
StreamReader strmReader = new StreamReader(inputFileName);
string text = System.IO.File.ReadAllText(@ "C:FilesMyFileName.txt");
Regex reg_exp = new Regex("[^a-zA-Z0-9]");
text = reg_exp.Replace(text, " ");
string[] words = text.Split(new char[] {
' '
}, StringSplitOptions.RemoveEmptyEntries);
var word_query = (from string word in words orderby word select word).Distinct();
string[] result = word_query.ToArray();
int counter = 0;
string delim = " ,.";
string[] fields = null;
string line = null;
while (!strmReader.EndOfStream) {
line = strmReader.ReadLine(); //Read a line and split it into the words
line.Trim();
fields = line.Split(delim.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
counter += fields.Length; //count
foreach(string word in result) {
CountStringOccurrences(text, word);
}
}
strmReader.Close();
Console.WriteLine("Total word count is {0}", counter);
Console.ReadLine();
}
//Count the frequency of word.
public static void CountStringOccurrences(string text, string word) {
int count = 0;
int i = 0;
while ((i = text.IndexOf(word, i)) != -1) {
i += word.Length;
count++;
}
Console.WriteLine("{0} {1}", count, word);
}
}