One of the popular programs which can be asked by the interviewer during the interview is to count no. of Vowels and Consonants in a string. The letters A, E, I, O, and U are called Vowels and the other letters (except vowels) in the alphabet are known as Consonants.
Console.WriteLine("Count
no. of Vowels and Consonants in a string"); //Variables to hold the Vowels and Consonants count int vCount = 0; int cCount = 0; string inputString = string.Empty; //Recieve the input from the user inputString = Console.ReadLine(); //Lower the characters of the string for the comparision inputString = inputString.ToLower(); //Loop through the each character of the string for (int i = 0; i < inputString.Length; i++) { //check and increase
the counter if character is vowel if (inputString[i]=='a' || inputString[i]=='e' || inputString[i] == 'i' || inputString[i] == 'o' || inputString[i] == 'u') { vCount++; } //check and increase the counter if character is consonant else if (inputString[i]>='a' && inputString[i] <= 'z') { cCount++; } } //Showing the Vowels and Constants count in a string to the user Console.WriteLine("Total
no. of Vowels in a string=" + vCount); Console.WriteLine("Total
no. of Constants in a string=" + cCount); Console.ReadLine(); |
0 comments:
Post a Comment