Check if a character is a vowel or consonant?

前端 未结 13 1156
别那么骄傲
别那么骄傲 2020-12-09 09:57

Is there a code to check if a character is a vowel or consonant? Some thing like char = IsVowel? Or need to hard code?

case ‘a’:
case ‘e’:
case ‘i’:
case ‘o’         


        
相关标签:
13条回答
  • 2020-12-09 10:43

    You could do this:

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

    or this:

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

    Once you add international support for things like éèe̋ȅëêĕe̊æøи etc. this string will get long, but the basic solution is the same.

    0 讨论(0)
  • 2020-12-09 10:47

    The other methods given work. Here I am concerned with performance. For the two approaches I tested - using LINQ's Any method and using bit arithmetic, the use of bit arithmetic was more than ten times faster. Results:

    Time for LINQ = 117 msec

    Time for Bit masks = 8 msec

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

    See https://dotnetfiddle.net/WbPHU4 for the code in a test with timings.

    The key idea with the bit mask is that the second bit is set for 'a', the sixth bit is set for 'e', etc. Then you take the letter, shift left by its ASCII value as an integer, and see if that bit in the mask is set. One bit is set in the mask for each vowel, and the OR operation performs the lowercasing of the letter first.

    0 讨论(0)
  • 2020-12-09 10:47

    Look at this code to check for both Vowel and 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");
    }
    
    0 讨论(0)
  • 2020-12-09 10:48
    return "aeiou".Any( c => c.Equals( Char.ToLowerInvariant( myChar ) ) );
    
    0 讨论(0)
  • 2020-12-09 10:49

    You can do something like this in.

      private bool IsCharacterAVowel(char c)
      {
         string vowels = "aeiou";
         return vowels.IndexOf(c.ToString(),StringComparison.InvariantCultureIgnoreCase) >= 0;      
      }
    
    0 讨论(0)
  • 2020-12-09 10:50

    No. You need to define first what you regard as a vowel and as a consonant. For example, in English, “y” could be a consonant (as in “yes”) or a vowel (as in “by”). Letters like “é” and “ü” are probably vowels in all languages in which they are used, but it seems that you did not consider them at all. Primarily, you should define why you wish to classify letters as consonants and vowels.

    0 讨论(0)
提交回复
热议问题