1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | public class FindOvelsConsonents { public static void main(String[] args) { // TODO Auto-generated method stub FindOvelsConsonents o = new FindOvelsConsonents(); o.findvowelsAndConsonents("NIHAL Is my Name"); o.findvowelsAndConsonentsUsingContains("NIHAL IS MY NAME"); } int count = 0, count1; // ===================Method: 1 ====================== void findvowelsAndConsonents(String str) { String s = str.toLowerCase(); System.out.println(s); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' || s.charAt(i) == 'o' || s.charAt(i) == 'u') { count++; } else if (s.charAt(i) == ' ') { } else { count1++; } } System.out.println("Ovels are: " + count + ", and Consonents are: " + count1); } // ========================Method:2 USING CONTAINS================== int vowels; int conso; void findvowelsAndConsonentsUsingContains(String str) { // int[] i = new int[256]; String strV = "aeiouAEIOU"; for (int j = 0; j < str.length(); j++) { if (strV.contains(Character.toString(str.charAt(j)))) { // Main Logic vowels++; // Incrementng vowels count } else if (str.charAt(j) == ' ') { } else { conso++; // Incrementng Consonents count } } System.out.println("Ovels are: " + vowels + " Consonents are: " + conso); } } |
Find Vowels and Consonants in given String
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment