Click here to Skip to main content
Full site     10M members (31.9K online)    

Counting how many times a letter appears in a string

Introduction

This Tip doesn't invent anything new, brilliant or fancy.
It just describes a little helper tool I wrote to learn a bit about the Caesar cipher, which I was told to get in touch with by my teacher. Pure basics to get into the whole Encryption/Decription thingie.
Since you need to know how many times a certain character appears in a string to crack a Caesar cipher encoded string, I'd have had to count every letter of a sentence.
Boring work, I tell ya. But I am an intelligent guy, and remembered that somewhen back at the start of my Apprenticeship we had to develop such a Tool ourselfs. Long story short, I lost the code for that  a while ago, and to make the redevelopment fun for me I decided to put this one up here.

Background

Well, you need to know about the Caesar cipher.  Wikipedia tells you the following about it:

In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence. 
As one can easily see, it is not a really great deal. Except that its hand-driven cracking is a real pain and needs a significant amount of time to be done.

Now - What can we do about it? Of course I can go trough every sentence and count the letters to find out which letter occurs the most. But that would be really inefficient.
And that is why I present you this little and nice program.

Another thingie you need to reckon is that I store the results in an integer array containing 26 fields.
Why 26 fields? I want to be able to process standard ASCII code letters. The ASCII code has exactly 52 possible letters and since I convert lowercase letters to uppercase I end up having 26 possible letters.
Of course, one can add lowercase support to my code, too. But for me it is unneccessary, at least currently.

'Nuff said, here we go and come to the interesting part:

Using the code

All code is placed in the main-method of the program, since adding further classes to it would have been a real overhead just for my simple use:

static void Main(string[] args)
        {
            WriteWelcomeText();
            string input = "";
            do
            {
                Console.WriteLine("Type the input string to count its letters, or \"##\" to exit the program");
                input = Console.ReadLine();
                char[] inputArray = input.ToUpper().ToArray();
                int[] characters = new int[26];
                for (int i = 0; i < input.Length; i++)
                {
                    char inputIndex = (char)(inputArray[i] - 65);
                    if(inputIndex >= 0 && inputIndex <= 26)//Check if character is a letter
                    {
                        characters[inputIndex]++;
                    }
                }
 
                Console.WriteLine();
                Console.WriteLine("Letter occurence table:");
                for (int i = 0; i < 26; i++)
                {
                    Console.WriteLine(string.Format("{0} occurs {1} times.", (char)(i + 65),  characters[i]));
                }
                Console.WriteLine();
                WriteCopyright();
                Console.WriteLine();
            } while (input != "##");
 
            Console.WriteLine("Thank you for using the Letter counting tool. You are now exiting the app.");
            Thread.Sleep(3000);
        }

The main Loop

The main loop (I want use the program multiple times without starting the .exe each time) is basically this:             

string input = "";
            do
            {
                Console.WriteLine("Type the input string to count its letters, or \"##\" to exit the program");
                input = Console.ReadLine();
            } while (input != "##");   

First of all, we have the string variable to store any input there (For obvious reasons, it is called input).
This main loop always asks for the input at the start...

Console.WriteLine("Type the input string to count its letters, or \"##\" to exit the program");
input = Console.ReadLine(); 

... and runs until the user types "##" as input.

 while (input != "##");   

The "##"-sequence to end the program is legit because the program only does count letters and not characters in a sentence at all. 


Console Window, program asks for the input string - Typing "##" ends the program 

Counting the letters

Not a big deal, actually. But as it is often the case I had to reckon the details to make it as simple as possible.

The actual letter count is stored into a 26 field integer array.
Why 26 fields? I want to be able to process standard ASCII code letters. The ASCII code has exactly 52 possible letters and since I convert  lowercase letters to uppercase I end up having 26 possible letters  which leads me to my 26 field integer array.

int[] characters = new int[26];

After reading the console input using Console.ReadLine the input is returned as char array, holding all the uppercase-converted characters of the original input string:

input = Console.ReadLine();
char[] inputArray = input.ToUpper().ToArray();

Afterwards a for-Loop goes trough every character of the input:

for (int i = 0; i < input.Length; i++)
{
    char inputIndex = (char)(inputArray[i] - 65);
    if(inputIndex >= 0 && inputIndex <= 26)//Check if character is a letter
    {
       characters[inputIndex]++;
    }
}

First of all, I calculate at which index of the integer array holding the letter counts the field must be incremented:

char inputIndex = (char)(inputArray[i] - 65);

Afterwards, I check if the calculated index is available in the integer array holding the letter counts:

if(inputIndex >= 0 && inputIndex <= 26)//Check if character is a letter
{
    characters[inputIndex]++;
}   

This step is necessary because the program wouldn't be able to process spaces and other special characters without crashing.

Output of the counted letters

The output of the calculated data is another for-Loop. The Loop goes through every field of the array which holds the count for a letter and prints out to the Console which letter the counter is referring to and the counter itself:

Console.WriteLine("Letter occurence table:");
for (int i = 0; i < 26; i++)
{
    Console.WriteLine(string.Format("{0} occurs {1} times.", (char)(i + 65),  characters[i]));
}

The only special thing here is that I add up 65 to the iterator i to get the letter to which the counter at the iterator position of the array is referring to:

(char)(i + 65) 

In the end, the program output for the sentence 'Creating this Tip/Trick was easy, but fun!' comes like that:


In the end I can say that it was fun to return to such a basic topic, and writing a Tip/Trick about it.
I hope you can use it for your own purpose!
Thanks for reading.

History 

Release dateComments
11-MAY-2013 Initial release



 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search 
Per page   
QuestionAn Alternative Suggestion.
George Swan
11 May '13 - 20:30 
AnswerRe: An Alternative Suggestion.
Marco Bertschi
11 May '13 - 23:18 
GeneralRe: An Alternative Suggestion.
George Swan
12 May '13 - 0:08 
AnswerRe: An Alternative Suggestion.
Marco Bertschi
12 May '13 - 4:15 
QuestionThoughts
PIEBALDconsult
11 May '13 - 8:18 
AnswerRe: Thoughts
Marco Bertschi
11 May '13 - 22:50 
GeneralRe: Thoughts
PIEBALDconsult
12 May '13 - 5:48 
GeneralRe: Thoughts
Marco Bertschi
12 May '13 - 5:55 
GeneralRe: Thoughts
PIEBALDconsult
12 May '13 - 5:57 
GeneralFormatting and a thought.
SoMad
11 May '13 - 7:27 
GeneralRe: Formatting and a thought.
Marco Bertschi
11 May '13 - 7:30 
GeneralRe: Formatting and a thought.
SoMad
11 May '13 - 7:44 
GeneralRe: Formatting and a thought.
Marco Bertschi
11 May '13 - 7:47 
GeneralRe: Formatting and a thought.
SoMad
11 May '13 - 7:56 
GeneralRe: Formatting and a thought.
Marco Bertschi
11 May '13 - 8:00 
GeneralRe: Formatting and a thought.
Marco Bertschi
11 May '13 - 7:33 

Last Updated 11 May 2013 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2013