Is a string a Pangram?


When is a string a pangram? Is actually fairly easy to check and beow you can see one implementation in C#.

var charsFound = new bool[26];
var s = Console.ReadLine().ToUpper();
 
// We need at least 26 characters (English alphabet) in the string...
if (s.Length >= 26) {
    
 
    foreach (var c in s)
        if (c >= 'A' && c <= 'Z') {
            // Uppercase ascii character codes starts at decimal index 65
            var index = c - 65;
            // Already marked, continue loop...
            if (charsFound[index]) continue;
 
            charsFound[index] = true;
        }
}
 
// Did we find all 26 chars?
Console.WriteLine(charsFound.Where(c => c).Count() == 26 ? "pangram" : "not pangram");
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s