-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask4_Program.cs
More file actions
76 lines (70 loc) · 2.13 KB
/
Task4_Program.cs
File metadata and controls
76 lines (70 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Task4
{
class Program
{
static void Main(string[] args)
{
//add logic here
StreamReader strReader = null;
SortedDictionary<char, int> myChars = new SortedDictionary<char, int>();
try
{
strReader = new StreamReader(@"..\..\characters.txt");
while (strReader.EndOfStream==false)
{
//Console.WriteLine((char)strReader.Read());
char c = (char)strReader.Read();
if (myChars.ContainsKey(c) == true)
{
myChars[c] = myChars[c] + 1;
}
else
{
myChars.Add(c, 1);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Something has gone wrong!");
Console.WriteLine(ex.Message.ToString());
}
finally
{
if (strReader != null)
{
strReader.Close();
}
}
//read myChars and write to text file
StreamWriter strWriter = null;
try
{
strWriter = new StreamWriter(@"..\..\character-count.txt", false);
foreach (KeyValuePair<char, int> kv in myChars)
{
strWriter.WriteLine($"{kv.Key} {kv.Value}");
}
}
catch (Exception ex)
{
Console.WriteLine("Something went wrong!");
Console.WriteLine(ex.Message.ToString());
}
finally
{
if (strWriter != null)
{
strWriter.Close();
}
}
Console.ReadKey();
}
}
}