Arianule
Arianule

Reputation: 9043

converting vowels in a list to uppercase

I have a char list of which I want to convert the vowels of this list to uppercase.

With the way I am doing it only returns the vowels and excludes all the consonants.

I know this is very elementary but how would I do this correctly.?

List<char> lstVowels = new List<char>() {'a', 'e', 'i', 'o', 'u' };

lstChar = lstChar.Where(p=>lstVowels.Contains(p)).Select(t => char.ToUpper(t)).ToList();
//lstChar contains a set of consonants and vowels

Upvotes: 3

Views: 1043

Answers (3)

Grant Winney
Grant Winney

Reputation: 66449

If a character exists in lstVowels then convert it to uppercase; otherwise, just keep it as-is.

lstChar =
    lstChar.Select(c => lstVowels.Contains(c) ? char.ToUpper(c) : c).ToList();

In your code, the Where clause causes you to lose all characters that don't exist in lstVowels.

Upvotes: 6

Sayse
Sayse

Reputation: 43300

personally I'd include an ternary if into your select instead

lstChar.Select(c => lstVowels.Contains(c) ? char.ToUpper(c) : c).ToList();

Upvotes: 3

Daniel Br&#252;ckner
Daniel Br&#252;ckner

Reputation: 59655

That's because you are filtering out consonants instead of just keeping them unchanged.

 lstChar.Select(c => lstVowels.Contains(c) ? Char.ToUpper(c) : c)

Upvotes: 1

Related Questions