Click here to Skip to main content
Click here to Skip to main content

Counting how many times a letter appears in a string

By , 11 May 2013
 

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



License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Marco Bertschi
Software Developer Apprenticeship
Switzerland Switzerland
Member
Marco made his first steps in programming with C# and the .NET framework. Afterwards he switched over to C++ development with Visual Studio and the Qt framework. Java is also a part of his experience but he ain't gonna use it if you do not force him with all your strength to do so.
 
As a part of the "generation Facebook" he grew up with social networks, video platforms as YouTube and Vimeo are, Smartphones and HD screens but also remembers the noisy annoying analog modems and Windows 95 including DOS games.
 
He never gets enough sleep and beside working in the R&D department of a international medical diagnostic company he is the guy behind the SMGT Web-Portal (A web application to help local shooting clubs organizing their events and staff based on ASP.NET and a MySQL server) and contributing articles to the CodeProject. If he isn't either coding or sleeping you can meet him at the Muay Thai training or somewhere else in central Switzerland.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
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 this forum  
    Spacing  Noise  Layout  Per page   
QuestionAn Alternative Suggestion.memberGeorge Swan11 May '13 - 20:30 
AnswerRe: An Alternative Suggestion.professionalMarco Bertschi11 May '13 - 23:18 
GeneralRe: An Alternative Suggestion.memberGeorge Swan12 May '13 - 0:08 
AnswerRe: An Alternative Suggestion.professionalMarco Bertschi12 May '13 - 4:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130513.1 | Last Updated 11 May 2013
Article Copyright 2013 by Marco Bertschi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid