检查字符串是否是全字母异序词的 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

更新日期:2020 年 6 月 19 日

2K+ 浏览量

开启您的职业生涯

完成课程取得认证

开始
广告