C# LAB: This problem is about the generic collection class SortedDictionary. Wri
ID: 3690143 • Letter: C
Question
C# LAB:
This problem is about the generic collection class SortedDictionary. Write a program to do this. Ask the user to enter a string. Convert every character into uppercase letter. Count and display the number of occurrences of every character. Ignore spaces. The following is an example.
Enter a string:
We few, we happy few, we band of brothers
Sorted dictionary contains:
Key: Value:
, 2
A 2
B 2
D 1
E 6
F 3
H 2
N 1
O 2
P 2
R 2
S 1
T 1
W 5
Y 1
Press any key to continue . . .
Explanation / Answer
plz check and let me konw your comments
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string strInput;
Console.WriteLine("Enter the String : ");
strInput = Console.ReadLine();
var count = CharCount.Count(strInput.ToUpper());
Console.WriteLine("Sorted dictionary contains: Key: Value: ");
foreach (var character in count)
{
if (character.Key != 32)
{
Console.WriteLine(" {0} {1}", character.Key, character.Value);
}
}
Console.Write("Press any key to continue . . .");
Console.ReadKey(true);
}
}
class CharCount
{
public static SortedDictionary<char, ulong> Count(string stringToCount)
{
SortedDictionary<char, ulong> charCount = new SortedDictionary<char, ulong>();
foreach (var character in stringToCount)
{
if (!charCount.ContainsKey(character))
{
charCount.Add(character, 1);
}
else
{
charCount[character]++;
}
}
return charCount;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.