C Programs Tutorials | IT Developer
IT Developer

Java Programs - Advanced



Share with a Friend

Rearrange Vowels & Consonants Program - Java Program

A class Rearrange has been defined to modify a word by bringing all the vowels in the word at the beginning followed by the consonants.

Example: ORIGINAL becomes OIIARGNL

Some of the members of the class are given below:

Class name: Rearrange
Data members/instance variables:
word: to store a word.
newWord: to store the rearranged word.

Member functions/methods:
Rearrange(): default constructor.
void readWord(): to accept the word in uppercase.
void freqVowelCon(): finds the frequency of vowels and consonants in the word and displays them with an appropriate message.
void arrange(): rearranges the word by bringing the vowels at the beginning followed by consonants.
void display(): displays the original word along with the rearranged word.

Specify the class Rearrange, giving details of the constructor, void readWord(), void freqVowelCon(), void arrange() and void display(). Define the main() function to create an object and call the functions accordingly to enable the task.

import java.io.*; class Rearrange{ private String word; private String newWord; public Rearrange(){ word = new String(); newWord = new String(); } public void readWord()throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the word: "); word = br.readLine(); } public void freqVowelCon(){ word = word.toUpperCase(); int v = 0; int c = 0; for(int i = 0; i < word.length(); i++){ char ch = word.charAt(i); if(Character.isLetter(ch)){ switch(ch){ case 'A': case 'E': case 'I': case 'O': case 'U': v++; break; default: c++; } } } System.out.println("Frequency of vowels: " + v); System.out.println("Frequency of consonants: " + c); } public void arrange(){ String v = new String(); String c = new String(); word = word.toUpperCase(); for(int i = 0; i < word.length(); i++){ char ch = word.charAt(i); if(Character.isLetter(ch)){ switch(ch){ case 'A': case 'E': case 'I': case 'O': case 'U': v += ch; break; default: c += ch; } } } newWord = v + c; } public void display(){ System.out.println("Original word: " + word); System.out.println("Rearranged word: " + newWord); } public static void main(String args[])throws IOException{ Rearrange obj = new Rearrange(); obj.readWord(); obj.freqVowelCon(); obj.arrange(); obj.display(); } }

Output

OUTPUT 1:
Enter the word: ORIGINAL
Frequency of vowels: 4
Frequency of consonants: 4
Original word: ORIGINAL
Rearranged word: OIIARGNL

OUTPUT 2:
Enter the word: ITDEVELOPER
Frequency of vowels: 5
Frequency of consonants: 6
Original word: ITDEVELOPER
Rearranged word: IEEOETDVLPR