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.
import java.util.*;
class VowelShift {
String wrd, newwrd;
int vow_cn = 0, oth_cn = 0;
VowelShift() {
}
void readword() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the word: ");
wrd = sc.next().toUpperCase();
sc.close();
}
void freq_vow_con() {
for (int x = 0; x < wrd.length(); x++)
if (is_vowel(wrd.charAt(x)))
vow_cn++;
else
oth_cn++;
System.out.println("Vowel count: " + vow_cn);
System.out.println("Consonant count: " + oth_cn);
}
void arrange() {
StringBuffer vw = new StringBuffer(""); // vowels
StringBuffer oth = new StringBuffer(""); // others
for (int x = 0; x < wrd.length(); x++) {
char c = wrd.charAt(x);
if (is_vowel(c))
vw.append(c);
else
oth.append(c);
}
newwrd = vw.toString() + oth.toString();
}
void display() {
System.out.println("Original Word: " + wrd);
System.out.println("Rearranged Word: " + newwrd);
}
boolean is_vowel(char val) {
String vw = "AEIOU";
return vw.indexOf(val) != -1;
}
public static void main(String[] args) {
VowelShift a = new VowelShift();
a.readword();
a.freq_vow_con();
a.arrange();
a.display();
}
}
Name | Type | Uses |
---|---|---|
global | ||
wrd | String | to store the entered word |
newwrd | String | to store changed(final) word |
vow_cn | int | to store vowel freequency |
oth_cn | int | to store other characters freequency |
void readword() | ||
sc | Scanner | object to take user input |
void freq_vow_con() | ||
x | int | counter variable to iterate over the string value |
void arrange() | ||
vw | StringBuffer | to store extracted vowels |
oth | StringBuffer | to store other extracted characters |
void main() | ||
a | VowelShift | to call the objects its class |