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

时间:2013-07-20 17:19:38

标签: c# character

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

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

13 个答案:

答案 0 :(得分:28)

你可以这样做:

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

或者这个:

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

一旦为éèe̋ȅëêĕe̊æøи等内容添加国际支持,此字符串将变长,但基本解决方案是相同的。

答案 1 :(得分:7)

这是一个有效的功能:

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!!! }

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

答案 2 :(得分:5)

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

答案 3 :(得分:3)

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);

答案 4 :(得分:1)

您可以根据需要使用“IsVowel”。然而,唯一的问题是可能没有默认的C#库或功能已经开箱即用,如果这是您想要的。您需要为此编写一个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;
}    

答案 5 :(得分:1)

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

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

答案 6 :(得分:1)

这很好用。

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();
    }
}

答案 7 :(得分:1)

其他方法给予了工作。在这里,我关注表现。对于我测试的两种方法 - 使用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操作首先执行字母的小写。

答案 8 :(得分:0)

您可以在。

中执行此类操作
  private bool IsCharacterAVowel(char c)
  {
     string vowels = "aeiou";
     return vowels.IndexOf(c.ToString(),StringComparison.InvariantCultureIgnoreCase) >= 0;      
  }

答案 9 :(得分:0)

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

答案 10 :(得分:0)

return "aeiou".Any( c => c.Equals( Char.ToLowerInvariant( myChar ) ) );

答案 11 :(得分:0)

试试这个:

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();

答案 12 :(得分:0)

查看此代码以检查Vowel和Consonant,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");
}
相关问题