I've been trying to solve this problem for the past 2days but can't figure how the consonants with the correct spacing. Your help will be greatly appreciated.
This is my output:
a I A
S m m .
package com.codegym.task.task09.task0923;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
Vowels and consonants
*/
public class Solution {
public static char[] vowels = new char[]{'a', 'e', 'i', 'o', 'u'};
public static void main(String[] args) throws Exception {
// write your code here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
String word = str.replaceAll("\\s+", "").trim();
char[] a = new char[word.length()];
for(int i = 0; i<word.length(); i++){
char c = word.charAt(i);
if(!isVowel(c)){
a[i] = c;
}
else{
System.out.print(c + " ");
}
}
System.out.println();
for(char i : a){
System.out.print(i+ " ");
}
}
// The method checks whether a letter is a vowel
public static boolean isVowel(char c) {
c = Character.toLowerCase(c); // Convert to lowercase
for (char d : vowels) // Look for vowels in the array
{
if (c == d)
return true;
}
return false;
}
}