In: Computer Science
write a c# program that:
The file is using a very simple form of compression in which
there are no spaces, each word is separated by camel casing. For
Example, the string "TheCatWillRun" is parsed as "The Cat Will
Run".
*Now for the statistics*
Prints to the console the following statistics about the content in
the file retrieved above.
- How many of each letter are in the file
- How many letters are capitalized in the file
- The most common word and the number of times it has been
seen.
- The most common 2 character prefix and the number of occurrences
in the text file.
////PLEASE READ ALL COMMENTS AND SEE THE FLOW and output
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace FileOperation
{
class Program
{
static void Main(string[] args)
{
string textFile =
"C:\\Users\\Nitesh.gupta\\source\\repos\\FileOperation\\FileOperation\\input.txt";
string[] lines = File.ReadAllLines(textFile);
int [] CapLetters = new int[26]; // AS there is total 26
alphabets
int[] SmallLetters = new int[26]; // AS there is total 26
alphabets
int TotalCapitalized = 0;
Dictionary<string, int> cd = new Dictionary<string,
int>(); // To get most common word
int startIndex = 0, lastIndex = 0;
foreach (string currentRow in lines) // Read each line
{
char[] cArray = currentRow.ToCharArray(); // Read each char
foreach(char currentChar in cArray)
{
//GET COUNT as per current letter is small or capital
if(currentChar >='a' && currentChar <='z')
{
SmallLetters[(int)currentChar - 97]++; // Decide small letter
count
}
else
{
CapLetters[(int)currentChar - 65]++; // get cap letter count
TotalCapitalized++; // Total capital letter count
string newString = currentRow.Substring(startIndex, lastIndex -
startIndex); //Read each word
if(lastIndex != 0)
{
if (cd.ContainsKey(newString)) cd[newString] += 1; // Insert into
dictionary to get distinct count
else cd.Add(newString, 1);
startIndex = lastIndex ;
}
}
lastIndex++;
}
if (lastIndex != startIndex) // If still words are there, add to
dictionary
{
string s = currentRow.Substring(startIndex, lastIndex -
startIndex);
if(cd.ContainsKey(s))
{
cd[s] += 1;
}
else
{
cd.Add(s, 1);
}
}
//string lastString = currentRow.Substring(startIndex,
lastIndex);
// cd.Add(lastString, cd.ContainsKey(lastString) ? 1 +
cd[lastString] : 1);
}
// Show the results
Console.WriteLine("\nAll count of each small letter from file is
");
for(int i = 0; i < 26; i++) // Show each letter count
{
Console.Write((char)(97 + i) + " -- " + SmallLetters[i]+" ");
}
Console.WriteLine("\nAll count of each capital letter
from file is ");
for (int i = 0; i < 26; i++)
{
Console.Write((char)(65 + i) + " -- " + CapLetters[i]+" ");
}
Console.WriteLine("There are total " + TotalCapitalized + "
capitalized letters");
Console.WriteLine("Most common words and there count: ");
foreach(KeyValuePair<string,int> kvp in cd)
{
if(kvp.Value > 1 )
{
Console.WriteLine(kvp.Key + "---" + kvp.Value + " times");
}
}
Console.WriteLine("The most common 2 character prefix
and the number of occurrences in the text file are
follwing");
foreach(KeyValuePair<string,int> kvp in cd)
{
if(kvp.Value >1)
{
Console.WriteLine(kvp.Key.Substring(0,2) + " --- " + kvp.Value + "
times");
}
}
Console.ReadKey();
}
}
}
/* THIS IS INPUT FILE CONTENTS */
TheCatWillRunBehindCat
/* This is output */