CSharp examples for Language Basics:char
Count vowels, consonants, digits, words and average word length
using System;/*from www .j a va 2 s . c o m*/ class TextAnalyzer { public static void Main() { string myText; int numVowels = 0; int numLetters = 0; int numDigits = 0; int numWhitespaceChars = 0; int numWords = 0; char ch; int index = 0; Console.WriteLine("Please enter text to be analyzed:"); myText = Console.ReadLine(); myText = myText.ToUpper(); while (index < myText.Length) { ch = myText[index]; if(ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' || ch == 'Y') numVowels++; if(char.IsLetter(ch)) numLetters++; if(char.IsDigit(ch)) numDigits++; if(char.IsWhiteSpace(ch)) numWhitespaceChars++; index++; } numWords = numWhitespaceChars + 1; Console.WriteLine("Text analysis:"); Console.WriteLine("Number of vowels: {0:N0}", numVowels); Console.WriteLine("Number of consonants: {0:N0}", (numLetters - numVowels)); Console.WriteLine("Number of letters: {0:N0}", numLetters); Console.WriteLine("Number or digits: {0:N0}", numDigits); Console.WriteLine("Number of words: {0:N0}", numWords); Console.WriteLine("Average word length: {0:N2}", ((numLetters + numDigits) / (float)numWords)); } }