Question

In: Computer Science

write a c# program that: The file is using a very simple form of compression in...

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.

Solutions

Expert Solution

////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 */


Related Solutions

Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++...
Using Virtualbox in Debian, write a simple program (a single .cpp file) in Linux shell C++ Rules: -Use fork(), exec(), wait(), and exit() _______________________________________________________________________________________________________________________________________________ -A line of input represents a token group. -Each token group will result in the shell forking a new process and then executing the process. e.g. cat –n myfile.txt // a token group -Every token group must begin with a word that is called the command(see example above). The words immediately following a command are calledarguments(e.g....
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Write a code for simple racing game (using dots) on c program.
Write a code for simple racing game (using dots) on c program.
Using C Write a program that will serve as a simple shell. This shell will execute...
Using C Write a program that will serve as a simple shell. This shell will execute an infinite for loop. In each iteration of the loop, the user will be presented with a prompt. When the user enters a command, the shell will tokenize the command, create a child process to execute it and wait for the child process to be over. If the user enters an invalid command, the shell should recognize the situation and show a meaningful message....
Write a JAVA program by making a LOGIN form using Excel file for validation. if user...
Write a JAVA program by making a LOGIN form using Excel file for validation. if user is found, display the user's First Name and Last Name.
C++ Code You will write a program to process the lines in a text file using...
C++ Code You will write a program to process the lines in a text file using a linked list and shared pointers. You will create a class “Node” with the following private data attributes: • Line – line from a file (string) • Next (shared pointer to a Node) Put your class definition in a header file and the implementation of the methods in a .cpp file. The header file will be included in your project. If you follow the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT