แพนแกรมมีตัวอักษรทั้งหมด 26 ตัว
ด้านล่าง เราได้ป้อนสตริง และจะตรวจสอบว่าเป็น pangram หรือไม่ -
string str = "The quick brown fox jumps over the lazy dog";
ตอนนี้ ให้ตรวจสอบโดยใช้ฟังก์ชัน ToLower(), isLetter() และ Count() ที่สตริงมีตัวอักษรทั้งหมด 26 ตัว ไม่ใช่เนื่องจาก pangram มีตัวอักษรทั้งหมด 26 ตัว
ตัวอย่าง
คุณสามารถลองเรียกใช้โค้ดต่อไปนี้เพื่อตรวจสอบว่าสตริงเป็น pangram หรือไม่
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Demo {
public class Program {
public static void Main(string []arg) {
string str = "The quick brown fox jumps over the lazy dog";
Console.WriteLine("{0}: \"{1}\" is pangram", checkPangram(str), str);
Console.ReadKey();
}
static bool checkPangram(string str) {
return str.ToLower().Where(ch => Char.IsLetter(ch)).GroupBy(ch => ch).Count() == 26;
}
}
} ผลลัพธ์
True: "The quick brown fox jumps over the lazy dog" is pangram