檢查字串是否是全字母異序詞的 C# 程式
全字母異序詞包含一個字母表中的 26 個字母。
下面,我們輸入了一個字串,並檢查它是否是一個全字母異序詞 −
string str = "The quick brown fox jumps over the lazy dog";
現在,使用 ToLower()、isLetter() 和 Count() 函式檢查字串是否包含字母表的 26 個字母,因為全字母異序詞包含一個字母表中的 26 個字母。
示例
你可以嘗試執行以下程式碼來檢查字串是否是全字母異序詞。
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
廣告