In: Computer Science
Please, read the code below so you can figure out what is the steps that are used. (Answer in words)
import java.util.*;
import java.io.*;
/
class WordHelper{
public static String vowels = "aeiouyAEIOUY";
//method to check whether a given character is vowel or not
public static String[] sortByVowels(String[] words){
Integer[] noOfVowels = new Integer[words.length];
String[] newArray = new String[words.length];
newArray[i] = words[i];
int cnt = 0;
for(int j = 0; j < words[i].length(); j++){
String temp = Character.toString(words[i].charAt(j));
if(vowels.contains(temp)){
cnt++;
}
}
noOfVowels[i] = cnt;
}
for(int i = 0; i < newArray.length; i++){
for(int j = 0; j < newArray.length - i - 1; j++){
if(noOfVowels[j] > noOfVowels[j+1]){
String temp = newArray[j];
newArray[j] = newArray[j+1];
newArray[j+1] = temp;
Integer tmp = noOfVowels[j];
noOfVowels[j] = noOfVowels[j+1];
noOfVowels[j+1] = tmp;
}
}
}
return newArray;
}
public static String[] sortByConsonants(String[] words){
Integer[] noOfConsonants = new Integer[words.length];
String[] newArray = new String[words.length];
for(int i = 0; i < words.length; i++){
newArray[i] = words[i];
int cnt = 0;
for(int j = 0; j < words[i].length(); j++){
String temp = Character.toString(words[i].charAt(j));
if(vowels.contains(temp)){
cnt++;
}
}
noOfConsonants[i] = words[i].length() - cnt;
}
for(int i = 0; i < newArray.length; i++){
for(int j = 0; j < newArray.length - i - 1; j++){
if(noOfConsonants[j] > noOfConsonants[j+1]){
String temp = newArray[j];
newArray[j] = newArray[j+1];
newArray[j+1] = temp;
Integer tmp = noOfConsonants[j];
noOfConsonants[j] = noOfConsonants[j+1];
noOfConsonants[j+1] = tmp;
}
}
}
return newArray;
}
public static String[] sortByLength(String[] words){
Integer[] length = new Integer[words.length];
String[] newArray = new String[words.length];
for(int i = 0; i < words.length; i++){
newArray[i] = words[i];
length[i] = words[i].length();
}
for(int i = 0; i < newArray.length; i++){
for(int j = 0; j < newArray.length - i - 1; j++){
if(length[j] > length[j+1]){
String temp = newArray[j];
newArray[j] = newArray[j+1];
newArray[j+1] = temp;
Integer tmp = length[j];
length[j] = length[j+1];
length[j+1] = tmp;
}
}
}
return newArray;
}
}
/* ADD YOUR CODE HERE */
After going through the code I have understand that you want to sort array of words given based on
1 .vowel count
2. consonant count
3. word length.
I wrote code for that and in the code below
initially takes total number of words for sorting , and reads those many words
later sorts based on all above 3 cases and prints the respective words respectively. go through below code
//java code starts from here
import java.util.*;
import java.io.*;
public class WordHelper {
public static String vowels =
"aeiouAEIOU";//vowels
public static void main(String[] args) {
Scanner scan = new
Scanner(System.in);
System.out.println("Enter number of
string to sort");
int totalWords = scan.nextInt();//
reading no of strings
System.out.println("Enter words
");
String words[] = new
String[totalWords];
for (int i = 0; i < totalWords;
i++) {
words[i] =
scan.next();// inserting into an array
}
String[] sort = null;
sort = sortByVowels(words);//
calling sort by vowel method
System.out.println("***********After sorting by
vowels************");
for (int i = 0; i < sort.length;
i++) {
System.out.println(sort[i]);
}
sort =
sortByConsonants(words);// calling sort by consonants method
System.out.println("***********After sorting by
consonants********");
for (int i = 0; i < sort.length;
i++) {
System.out.println(sort[i]);
}
sort = sortByLength(words);//
calling sort by length method
System.out.println("***********After sorting by
length*********");
for (int i = 0; i < sort.length;
i++) {
System.out.println(sort[i]);
}
}
public static String[] sortByVowels(String[] words) {
// initailizing arrays based on
sizes
Integer[] noOfVowels = new
Integer[words.length];
String[] newArray = new
String[words.length];
// calculating vowels length for
every string and storing into array on index
// based
for (int i = 0; i <
words.length; i++) {
int count =
0;
for (int j = 0;
j < words[i].length(); j++) {
if (vowels.contains(words[i].charAt(j) + ""))
{
count++;
}
}
newArray[i] =
words[i];
noOfVowels[i] =
count;
}
// comparing vowels length and
sorting and interchanging based on indexes
for (int i = 0; i <
noOfVowels.length; i++) {
for (int j = i +
1; j < noOfVowels.length; j++) {
if (noOfVowels[i] > noOfVowels[j]) {
String temp =
newArray[i];
newArray[i] =
newArray[j];
newArray[j] = temp;
}
}
}
return newArray;
}
public static String[] sortByConsonants(String[]
words) {
// initailizing arrays based on
sizes
Integer[] noOfConsonants = new
Integer[words.length];
String[] newArray = new
String[words.length];
// calculating vowels length for
every string and storing into array on index
// based
for (int i = 0; i <
words.length; i++) {
int count =
0;
for (int j = 0;
j < words[i].length(); j++) {
if (!vowels.contains(words[i].charAt(j) + ""))
{
count++;
}
}
newArray[i] =
words[i];
noOfConsonants[i] = count;
}
// comparing vowels length and
sorting and interchanging based on indexes
for (int i = 0; i <
noOfConsonants.length; i++) {
for (int j = i +
1; j < noOfConsonants.length; j++) {
if (noOfConsonants[i] > noOfConsonants[j])
{
String temp =
newArray[i];
newArray[i] =
newArray[j];
newArray[j] = temp;
}
}
}
return newArray;
}
public static String[] sortByLength(String[] words) {
String[] newArray = new
String[words.length];
for (int i = 0; i <
words.length; i++) {
newArray[i] =
words[i];
}
// using bubble sort for sorting
based on length
for (int i = 0; i <
newArray.length; i++) {
for (int j = i +
1; j < newArray.length; j++) {
if (newArray[i].length() >
newArray[j].length()) {
String temp =
newArray[j];
newArray[j] = words[i];
newArray[i] = temp;
}
}
}
return words;
}
}