3.8. Studio: Counting Characters

In this studio, you will write a program to count the number of times each character occurs in a string and then print the results to the console.

3.8.1. Getting Started

Create your own project from scratch. Review Creating a C# Project if you need to.

3.8.2. Some Items to Ponder Before Coding

  1. There are multiple ways to approach this task, but one way involves the following steps:
    1. Loop through the string one character at a time,
    2. Store and/or update the count for a given character using an appropriate data structure.
    3. Loop through the data structure to print the results (one character and its count per line).
  2. Which type of data structure (List, array, or Dictionary) should you use to store character counts? Any of these options would work, but using one of these data structures will optimize your code’s efficiency.
  3. You’ll need to initialize the counts for the characters in some fashion. It’s probably better to do this as you go through the string instead of doing so before you loop through it. (WHY?)
  4. If you need to review how to create a new class, revisit the Hello Methods program.
  5. Don’t forget to check out the Bonus Missions below.

Tip

Remember, you can turn a string object into an array of characters using:

char[] charactersInString = myString.ToCharArray();

3.8.3. Sample Input

Feel free to prompt the user for a string. However, for the sake of simplicity, you might want to start by hard-coding some text and storing it in a variable. For your convenience, here is some lorem ipsum text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc accumsan sem ut ligula scelerisque sollicitudin. Ut at sagittis augue. Praesent quis rhoncus justo. Aliquam erat volutpat. Donec sit amet suscipit metus, non lobortis massa. Vestibulum augue ex, dapibus ac suscipit vel, volutpat eget massa. Donec nec velit non ligula efficitur luctus.

3.8.4. Sample Output

For the example string above, your output should look something like:

L: 1
o: 15
r: 9
e: 26
m: 11
 : 50
i: 27
p: 7
s: 29
u: 28
d: 4
l: 17
t: 29
a: 22
,: 4
c: 17
n: 14
g: 7
.: 8
N: 1
q: 3
U: 1
P: 1
h: 1
j: 1
A: 1
v: 4
D: 2
b: 3
V: 1
x: 1
f: 2

3.8.5. Bonus Missions

Try these modifications on your code:

  1. Prompt the user to enter the string in the command line.
  2. Make the character counts case-insensitive.
  3. Exclude non-alphabetic characters.

3.8.5.1. Super Bonus

Read the string in from a file.

Note

This is a hard one. We won’t talk about reading from files in C# in this course, so be ready for a tough challenge if you accept this mission.