Posts

Showing posts with the label Vowels

Count vowels, consonants, digits and other characters in Java

Image
import java.util.Scanner; public class question1{ public static void main(String[]args){ Scanner input = new Scanner(System.in); System.out.print("Enter a string: "); String line = input.nextLine(); int cons = 0, vowe = 0, digi = 0, othr = 0, coun = 0; for(char c: line.toCharArray()){ coun ++; if(Character.isLetter(c)){ switch(c){ case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': vowe ++; break; default: cons ++; } } else if(Character.isDigit(c)){ digi ++; } else { othr ++; } } System.out.printf( "%d consonants.%n%d vowels.%n%d numbers.%n%d other characters.%n%d total characters.%n" , cons, vowe, digi, othr, coun ); } } Download

Counting vowels in a string with C Sharp

Image
using System; public class CountVowels{ public static void Main(string[]args){ Console.Write("Enter a phrase: "); string line = Console.ReadLine(); if(null == line){ Console.WriteLine("\nError: Missing input phrase!"); return; } int vowels = 0; foreach(char c in line){ switch(c){ case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': vowels ++; break; } } Console.WriteLine("{0} vowels found in phrase: \"{1}\"", vowels, line); } } Download