Java program Vowels and Consonants splitter - switch case char
The java code splits a string into two string object containing vowels and consonants character.On executing the code, an input has a string and it is converted into array of char. and then each character is passed into switch case to check whether it is a vowel or consonant and stored into a string object separately.
Output
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SwitchCase {
public static void main(String[] args) throws IOException
{
InputStreamReader dis =new InputStreamReader ( System.in );
BufferedReader br =new BufferedReader(dis);
String tstr ="Enter a string sentence to split
vowels and consonants";
System.out.println(tstr);
String arg1 =br.readLine();
char carr[]= arg1.toLowerCase().toCharArray();
StringBuffer vowels=new StringBuffer();
StringBuffer consnt=new StringBuffer();
for (int n=0;n<carr.length;n++)
{
switch ( carr[n] ) {
case 'a':
vowels.append(carr[n]);
break;
case 'e':
vowels.append(carr[n]);
break;
case 'i':
vowels.append(carr[n]);
break;
case 'o':
vowels.append(carr[n]);
break;
case 'u':
vowels.append(carr[n]);
break;
default :
consnt.append(carr[n]);
}
}
System.out.println("nVowels chars :" + vowels);
System.out.println("Consonant chars :" + consnt);
}
}
Output
Enter a string sentence to separate vowels and consonants java hello world Vowels chars :aaeoo Consonant chars :jv hll wrld
Related Post
Arithmetic Operation - Switch case constant string
Vowels and Consonants splitter - switch case with conditional char
String object member function access - switch case with condition constant integer
Arithmetic Operation - Switch case constant string
Vowels and Consonants splitter - switch case with conditional char
String object member function access - switch case with condition constant integer
Comments
Post a Comment