Pangrams in .NET

Introduction

Letters form words. Words form sentences. Sentences form paragraphs. Paragraphs form books. Books form libraries. Each language makes use of its own letters and symbols, but each language does have similarities when it comes to writing.

Each language contains vowels and consonants and basic language elements. Each language also contains “special” sentences or words. Today, I would like to introduce to you the pangram and how to test whether a sentence is a pangram in your .NET applications. Obviously, there are many more “special” words and sentences, and I will give a little background on those that I know of as well.

Pangram

The word pangram comes from the Greek phrase: pan gramma, which means “every letter“. A pangram (or holoalphabetic sentence) is a sentence that makes use of every letter of the alphabet at least once.

The sentence “The quick brown fox jumps over the lazy dog.” is the best known pangram and is usually associated with displaying font styles. The sentence has actually been used since at least the late 19th century. Some pangrams make less sense as people try to use only the 26 letters of the alphabet. The longer the pangram, the more it makes sense; the shorter, not so much. Here is an example of both:

Short

Bright vixens jump; dozy fowl quack.

Long

Jim quickly realized that the beautiful gowns are expensive.

The short sentence is understandable, but seems forced whereas the longer sentence is quite understandable. For a decent list of pangrams in the English language, have a look here.

Lipogram

The word lipogram comes from the Greek phrase leipográmmatos, which means “leaving out a letter.” A lipogram is a type of constrained writing in which a particular letter or group of letters is avoided. Albert Ross Eckler Jr. rewrote the famous poem “Mary had a little lamb” a few times with each time omitting a crucial letter, as seen next:

Original

Mary had a little lamb
Its fleece was white as snow
And everywhere that Mary went
The lamb was sure to go
 
He followed her to school one day
That was against the rule
It made the children laugh and play
To see the lamb in school

Missing “E”:

Mary had a tiny lamb
Its wool was pallid as snow
And any spot that Mary did walk
This lamb would always go
 
This lamb did follow Mary to school
Although against a law
How girls and boys did laugh and play
That lamb in class all saw

Missing “H”:

Mary owned a little lamb
Its fleece was pale as snow
And every place its mistress went
It would certainly go
 
It followed Mary to class one day It broke a rigid law
It made some students giggle aloud
A lamb in class all saw

Anagram

An anagram is a word, phrase, or sentence formed by rearranging the letters of a different word or phrase, while using all the original letters. Some examples include:

  • arc = car
  • state = taste
  • dusty = study
  • Night = thing
  • inch = chin
  • brag = grab
  • cat = act
  • stressed = desserts
  • dormitory = dirty Room
  • a decimal point = I’m a dot in place

Isogram

An isogram (non-pattern word) is a word or phrase without a repeating letter. A few examples follow:

  • campground
  • disturbance
  • incomputable
  • unsympathized
  • hydropneumatic
  • subdermatoglyphic

Heterogram

A heterogram (different written) is a word, phrase, or sentence in which no letter of the alphabet occurs more than once. Some examples:

  • certainly
  • flowchart
  • overmatch
  • prelaunch
  • replaying
  • squawking
  • traveling
  • vectorial

Other

There are more “special” words, phrases and sentences like the following:

Our Project

The aim of today’s little project is to determine whether or not the text input makes a pangram. It will also show the letters that have not been used.

Open Visual Studio and create either a C# or a Visual Basic.NET Windows Forms application. Design your form as shown in Figure 1.

Design
Figure 1: Design

The form contains one TextBox and two Labels. The labels’ Text will be changed once the program is running.

Import the System.Text namespace to work with StringBuilders:

C#

using System.Text;

VB.NET

Imports System.Text

Declare the following objects:

C#

      private string strLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      private string strPunctuation = " .,-:;!?'\"()[]{}/";
      private StringBuilder sbPangram = new StringBuilder();

VB.NET

   Private strLetters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   Private strPunctuation As String = " .,-:;!?'""()[]{}/"
   Private sbPangram As New StringBuilder

Here, you create objects to hold the English alphabet, punctuation marks, and create the StringBuilder object that will hold the pangram.

Add the following code inside the TextBox’s TextChanged event.

C#

      private void txtPangram_TextChanged(object sender,
         EventArgs e)
      {

         StringBuilder sbMissing = new StringBuilder();
         sbPangram = new StringBuilder(txtPangram.Text.ToUpper());

         foreach (char cp in strPunctuation)

            sbPangram.Replace(cp.ToString(), "".ToString());
         lblLetterCount.Text = "Total Letters = " +
            sbPangram.Length.ToString();

         foreach (char ch in strLetters.ToCharArray())
         {
            if (!sbPangram.ToString().Contains(ch))
            {
               sbMissing.Append(ch);
               sbMissing.Append(" ");
            }
         }

         lblMissing.Text = sbMissing.ToString();

      }

VB.NET

   Private Sub txtPangram_TextChanged(sender As Object, _
         e As EventArgs) Handles txtPangram.TextChanged

      Dim sbMissing As New StringBuilder
      sbPangram = New StringBuilder(txtPangram.Text.ToUpper)

      For Each cp As Char In strPunctuation

         sbPangram.Replace(cp, "")

      Next
      lblLetterCount.Text = "Total Letters = " &
         sbPangram.Length.ToString

      For Each ch As Char In strLetters.ToCharArray

         If Not sbPangram.ToString.Contains(ch) Then

            sbMissing.Append(ch)
            sbMissing.Append(" ")

         End If

      Next

      lblMissing.Text = sbMissing.ToString

   End Sub

You created a new StringBuilder object to hold the letters still needed to complete the pangram. You then changed the case of the pangram to all uppercase and replaced any punctuation marks with an empty string.

In the For Each loop, you determined which letters are still needed to complete the pangram and display them on the label named lblMissing.

Figure 2 shows the program in action.

Running
Figure 2: Running

Conclusion

Not only did you learn a lot about the English language, you also learned how to manipulate your .NET code to manipulate the English language. I hope you have found today’s article interesting. I do like to explain non-technical stuff too, as you saw, but by doing that it provides a greater context to the .NET code.

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read