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:
wrd: to store a word
newwrd: to store the rearranged word
Member functions/methods:
Rearrange(): default constructor
void readword(): to accept the word in uppercase
void freq_vow_con(): 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 freq_vow_con(), void arrange() and void display(). Define the main() function to create an object and call the functions accordingly to enable the task.
import java.util.Scanner;
class Rearrange{
String wrd;
String newwrd;
public Rearrange(){
wrd = "";
newwrd = "";
}
public void readword(){
Scanner in = new Scanner(System.in);
System.out.print("Enter the word: ");
wrd = in.next().toUpperCase();
}
public void freq_vow_con(){
int v = 0;
int c = 0;
for(int i = 0; i < wrd.length(); i++){
char ch = wrd.charAt(i);
if("AEIOU".indexOf(ch) >= 0)
v++;
else if(Character.isLetter(ch))
c++;
}
System.out.println("Number of vowels: " + v);
System.out.println("Number of consonants: " + c);
}
public void arrange(){
int pos = 0;
for(int i = 0; i < wrd.length(); i++){
char ch = wrd.charAt(i);
if("AEIOU".indexOf(ch) >= 0){
newwrd = newwrd.substring(0, pos) + ch + newwrd.substring(pos);
pos++;
}
else if(Character.isLetter(ch))
newwrd += ch;
}
}
public void display(){
System.out.println("Original word: " + wrd);
arrange();
System.out.println("Rearranged word: " + newwrd);
}
public static void main(String[] args){
Rearrange obj = new Rearrange();
obj.readword();
obj.freq_vow_con();
obj.display();
}
}