Separating Consonants and Vowels in a String in Java

A class Consonants has been defined with the following details:

Class name: Consonants
Data members:
word: stores the word.
len: stores the length of the word.
Member functions:
Consonants(): default constructor.
void read(): stores the word and the length of the word.
void shifts(): shifts all the consonants of the word at the beginning followed by the vowels (for example, Computer becomes cmptroue)
void change(): changes the case of all occurring consonants of the shifted word to uppercase (for example, cmptroue becomes CMPTRoue)
void show(): display the original word, shifted word and the changed word.

Specify class Consonants, giving details of the constructor, read(), shifts(), change(), and show(). Define the main() function to create an object and call the functions accordingly to enable the task.

Program:

import java.io.*;
class Consonants{
    private String word;
    private int len;
    public Consonants(){
        word = new String();
        len = 0;
    }
    public void read()throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Word: ");
        word = br.readLine();
        word = word.trim();
        if(word.indexOf(' ') > 0)
            word = word.substring(0, word.indexOf(' '));
        word = word.toLowerCase();
        len = word.length();
    }
    public void shifts(){
        String v = new String();
        String c = new String();
        for(int i = 0; i < len; i++){
            char ch = word.charAt(i);
            switch(ch){
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                v += ch;
                break;
                default:
                c += ch;
            }
        }
        word = c + v;
    }
    public void change(){
        String c = new String();
        for(int i = 0; i < len; i++){
            char ch = word.charAt(i);
            switch(ch){
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                c += ch;
                break;
                default:
                c += Character.toUpperCase(ch);
            }
        }
        word = c;
    }
    public void show(){
        System.out.println("Original word: " + word);
        this.shifts();
        System.out.println("Shifted word: " + word);
        this.change();
        System.out.println("Changed word: " + word);
    }
    public static void main(String args[])
    throws IOException{
        Consonants obj = new Consonants();
        obj.read();
        obj.show();
    }
}

Leave a Reply

%d bloggers like this: