C# program to check if string is panagram or not

A pangram is a sentence that contains all 26 letters of the English alphabet at least once. The word "pangram" comes from Greek, meaning "all letters". A famous example is "The quick brown fox jumps over the lazy dog".

In C#, we can check if a string is a pangram by converting it to lowercase, filtering only alphabetic characters, and counting the distinct letters present.

Syntax

The basic approach uses LINQ methods to process the string −

string.ToLower().Where(ch => Char.IsLetter(ch)).GroupBy(ch => ch).Count()

This checks if the count of distinct letters equals 26.

Using LINQ for Pangram Detection

This method uses Where() to filter letters, GroupBy() to group identical characters, and Count() to count distinct groups −

using System;
using System.Linq;

namespace Demo {
    public class Program {
        public static void Main(string[] args) {
            string str1 = "The quick brown fox jumps over the lazy dog";
            string str2 = "Hello World";
            string str3 = "Pack my box with five dozen liquor jugs";
            
            Console.WriteLine("'{0}' is pangram: {1}", str1, checkPangram(str1));
            Console.WriteLine("'{0}' is pangram: {1}", str2, checkPangram(str2));
            Console.WriteLine("'{0}' is pangram: {1}", str3, checkPangram(str3));
        }
        
        static bool checkPangram(string str) {
            return str.ToLower().Where(ch => Char.IsLetter(ch)).GroupBy(ch => ch).Count() == 26;
        }
    }
}

The output of the above code is −

'The quick brown fox jumps over the lazy dog' is pangram: True
'Hello World' is pangram: False
'Pack my box with five dozen liquor jugs' is pangram: True

Using HashSet for Efficient Checking

An alternative approach uses a HashSet to store unique characters, which can be more efficient for large strings −

using System;
using System.Collections.Generic;

namespace Demo {
    public class Program {
        public static void Main(string[] args) {
            string[] testStrings = {
                "The five boxing wizards jump quickly",
                "How vexingly quick daft zebras jump",
                "This is not a pangram"
            };
            
            foreach (string test in testStrings) {
                Console.WriteLine("'{0}' is pangram: {1}", test, isPangramHashSet(test));
            }
        }
        
        static bool isPangramHashSet(string str) {
            HashSet<char> letters = new HashSet<char>();
            
            foreach (char c in str.ToLower()) {
                if (char.IsLetter(c)) {
                    letters.Add(c);
                }
            }
            
            return letters.Count == 26;
        }
    }
}

The output of the above code is −

'The five boxing wizards jump quickly' is pangram: True
'How vexingly quick daft zebras jump' is pangram: True
'This is not a pangram' is pangram: False

Using Boolean Array for Character Tracking

This method uses a boolean array to mark the presence of each letter, providing excellent performance −

using System;

namespace Demo {
    public class Program {
        public static void Main(string[] args) {
            string sentence = "Amazingly few discotheques provide jukeboxes";
            
            Console.WriteLine("Testing: '{0}'", sentence);
            Console.WriteLine("Is pangram: {0}", isPangramArray(sentence));
            
            if (isPangramArray(sentence)) {
                Console.WriteLine("This sentence contains all 26 letters!");
            } else {
                Console.WriteLine("Missing letters found.");
            }
        }
        
        static bool isPangramArray(string str) {
            bool[] present = new bool[26];
            int lettersFound = 0;
            
            foreach (char c in str.ToLower()) {
                if (char.IsLetter(c)) {
                    int index = c - 'a';
                    if (!present[index]) {
                        present[index] = true;
                        lettersFound++;
                        if (lettersFound == 26) return true;
                    }
                }
            }
            
            return lettersFound == 26;
        }
    }
}

The output of the above code is −

Testing: 'Amazingly few discotheques provide jukeboxes'
Is pangram: True
This sentence contains all 26 letters!

Comparison of Methods

Method Time Complexity Space Complexity Advantages
LINQ GroupBy O(n) O(26) Concise, readable code
HashSet O(n) O(26) Good performance, clear logic
Boolean Array O(n) O(26) Best performance, early exit

Conclusion

Checking if a string is a pangram involves verifying that all 26 English letters appear at least once. The LINQ approach is most concise, HashSet provides good readability, while the boolean array method offers the best performance with early termination when all letters are found.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements