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

更新日期:19-6 月-2020

2K+ 瀏覽量

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.