检查一个字符是元音还是辅音?

是否有代码来检查角色是元音还是辅音? 像char = IsVowel这样的东西? 还是需要硬编码?

case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': 

你可以这样做:

 char c = ... bool isVowel = "aeiouAEIOU".IndexOf(c) >= 0; 

或这个:

 char c = ... bool isVowel = "aeiou".IndexOf(c.ToString(), StringComparison.InvariantCultureIgnoreCase) >= 0; 

一旦你为éèe̋ȅëêĕe̊æøи等事物添加国际支持,这个字符串就会变长,但基本的解决方案是相同的。

这是一个有效的function:

 public static class CharacterExtentions { public static bool IsVowel(this char c) { long x = (long)(char.ToUpper(c)) - 64; if (x*x*x*x*x - 51*x*x*x*x + 914*x*x*x - 6894*x*x + 20205*x - 14175 == 0) return true; else return false; } } 

使用它像:

 char c = 'a'; if (c.IsVowel()) { // it's a Vowel!!! } 

(是的,它确实有效 ,但很明显,这是一个笑话答案。不要贬低我。或其他什么。)

不需要。您需要首先定义您认为的元音和辅音。 例如,在英语中,“y”可以是辅音(如“是”)或元音(如“by”)。 像“é”和“ü”这样的字母可能是使用它们的所有语言中的元音,但似乎你根本不考虑它们。 首先,您应该定义为什么要将字母分类为辅音和元音。

 Console.WriteLine("Please input a word or phrase:"); string userInput = Console.ReadLine().ToLower(); for (int i = 0; i < userInput.Length; i++) { //c stores the index of userinput and converts it to string so it is readable and the program wont bomb out.[i]means position of the character. string c = userInput[i].ToString(); if ("aeiou".Contains(c)) { vowelcount++; } } Console.WriteLine(vowelcount); 

您可以根据需要使用“IsVowel”。 然而,唯一的问题是可能没有默认的C#库或function已经开箱即用,如果这是您想要的。 您需要为此编写一个util方法。

 bool a = isVowel('A');//example method call public bool isVowel(char charValue){ char[] vowelList = {'a', 'e', 'i', 'o', 'u'}; char casedChar = char.ToLower(charValue);//handle simple and capital vowels foreach(char vowel in vowelList){ if(vowel == casedChar){ return true; } } return false; } 

您可以使用以下扩展方法:

 using System; using System.Linq; public static class CharExtentions { public static bool IsVowel(this char character) { return new[] {'a', 'e', 'i', 'o', 'u'}.Contains(char.ToLower(character)); } } 

使用它像:

 'c'.IsVowel(); // Returns false 'a'.IsVowel(); // Returns true 

这很好用。

 public static void Main(string[] args) { int vowelsInString = 0; int consonants = 0; int lengthOfString; char[] vowels = new char[5] { 'a', 'e', 'i', 'o', 'u' }; string ourString; Console.WriteLine("Enter a sentence or a word"); ourString = Console.ReadLine(); ourString = ourString.ToLower(); foreach (char character in ourString) { for (int i = 0; i < vowels.Length; i++) { if (vowels[i] == character) { vowelsInString++; } } } lengthOfString = ourString.Count(c => !char.IsWhiteSpace(c)); //gets the length of the string without any whitespaces consonants = lengthOfString - vowelsInString; //Well, you get the idea. Console.WriteLine(); Console.WriteLine("Vowels in our string: " + vowelsInString); Console.WriteLine("Consonants in our string " + consonants); Console.ReadKey(); } } 

其他方法给出了工作。 在这里,我关注表现。 对于我测试的两种方法 – 使用LINQ的Any方法并使用位算术,位运算的使用速度提高了十倍以上。 结果:

LINQ的时间= 117毫秒

位掩码的时间= 8毫秒

 public static bool IsVowelLinq(char c) { char[] vowels = new[] { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' }; return vowels.Any(ch => ch == c); } private static int VowelMask = (1 << 1) | (1 << 5) | (1 << 9) | (1 << 15) | (1 << 21); public static bool IsVowelBitArithmetic(char c) { // The OR with 0x20 lowercases the letters // The test c > 64 rules out punctuation, digits, and control characters. // An additional test would be required to eliminate characters above ASCII 127. return (c > 64) && ((VowelMask & (1 << ((c | 0x20) % 32))) != 0); } 

有关时间的测试中的代码,请参阅https://dotnetfiddle.net/WbPHU4 。

位掩码的关键思想是第二位设置为'a',第六位设置为'e'等。然后你取字母,向左移动其ASCII值作为整数,看看是否掩码中的那个位被设置。 在每个元音的掩码中设置一位,OR操作首先执行字母的小写。

为什么不创建一个元音/辅音数组并检查值是否在数组中?

你可以做这样的事情。

  private bool IsCharacterAVowel(char c) { string vowels = "aeiou"; return vowels.IndexOf(c.ToString(),StringComparison.InvariantCultureIgnoreCase) >= 0; } 
 return "aeiou".Any( c => c.Equals( Char.ToLowerInvariant( myChar ) ) ); 

试试这个:

 char[] inputChars = Console.ReadLine().ToCharArray(); int vowels = 0; int consonants = 0; foreach (char c in inputChars) { if ("aeiou".Contains(c) || "AEIOU".Contains(c)) { vowels++; } else { consonants++; } } Console.WriteLine("Vowel count: {0} - Consonant count: {1}", vowels, consonants); Console.ReadKey(); 

看看这段代码来检查元音和辅音,C#

 private static void Vowel(string value) { int vowel = 0; foreach (var x in value.ToLower()) { if (x.Equals('a') || x.Equals('e') || x.Equals('i') || x.Equals('o') || x.Equals('u')) { vowel += 1; } } Console.WriteLine( vowel + " number of vowels"); } private static void Consonant(string value) { int cont = 0; foreach (var x in value.ToLower()) { if (x > 'a' && x <= 'd' || x > 'e' && x < 'i' || x > 'j' && x < 'o' || x >= 'p' && x < 'u' || x > 'v' && x < 'z') { cont += 1; } } Console.WriteLine(cont + " number of consonant"); }